Turn on thread page Beta
 You are Here: Home

# help with 2 arrays watch

1. when one input is made which in in one array, the other array should be assigned to it e.g.
compo= ['h','e','l'],['l','o']
price= ['10','20','30'],['40','50']
when h is inputed price= 10, is there any way of doing this? I'm trying in python
many thanks!
2. Yes there are many ways to do this! (Typically these "nested" lists which contain other lists are called a two-dimensional lists, by the way - this might help you know what to google for if you want to try finding other similar examples)! What have you tried so far?

It would be easier to help you in wherever you're stuck If you show the code you've tried.

If you need to check the Python syntax or see some examples using two-dimensional lists - have a look here:
https://snakify.org/lessons/two_dime..._lists_arrays/
3. (Original post by winterscoming)
Yes there are many ways to do this! (Typically these "nested" lists which contain other lists are called a two-dimensional lists, by the way - this might help you know what to google for if you want to try finding other similar examples)! What have you tried so far?

It would be easier to help you in wherever you're stuck If you show the code you've tried.

If you need to check the Python syntax or see some examples using two-dimensional lists - have a look here:
https://snakify.org/lessons/two_dime..._lists_arrays/
thats great thanks so much
4. I want to be able for a user to input one of the p3, p5 or p7 and the price, either 100,120 or 200 be be the price of that component
5. Out of interest, is there a reason you're using multiple lists like this? I mean, what you're doing will work, but there might be a more 'sane' alternative in using a dictionary to join a component and a price together, and to make the whole thing a bit easier for you (and someone else..) to understand..

(See the Python docs on dictionaries - https://docs.python.org/3/tutorial/d...l#dictionaries

There are some similarities with dictionaries and lists, but using a dictionary, you can combine fields together which are associated with each other, but also use names instead of numbers - like this:
Code:
components = {
'processors': [
{ 'name': 'p3',  'price': 100},
{ 'name': 'p5',  'price': 120},
{ 'name': 'p7',  'price': 200}
],
'memory': [
{ 'name': '16GB',  'price': 75},
{ 'name': '32GB',  'price': 150}
],
'storage': [
{ 'name': '1TB',  'price': 50},
{ 'name': '2TB',  'price': 100}
]
}

my_p3 = components['processors'][0]

print(my_p3['name'] + ' ' + str(my_p3['price']))
instead of keeping separate lists, this combines both together, but there are still lists nested inside the dictionary, although the data is grouped together in a way which looks a bit more 'human' (using strings instead of numbers, and putting each component closer to its price).

The problem with writing separate lists for your prices and your component names is that you need to keep switching between those lists, which doesn't seem very intuitive to me. This way of grouping things might feel a bit more intuitive to you hopefully!
6. (Original post by winterscoming)
Out of interest, is there a reason you're using multiple lists like this? I mean, what you're doing will work, but there might be a more 'sane' alternative in using a dictionary to join a component and a price together, and to make the whole thing a bit easier for you (and someone else..) to understand..

(See the Python docs on dictionaries - https://docs.python.org/3/tutorial/d...l#dictionaries

There are some similarities with dictionaries and lists, but using a dictionary, you can combine fields together which are associated with each other, but also use names instead of numbers - like this:
Code:
components = {
'processors': [
{ 'name': 'p3',  'price': 100},
{ 'name': 'p5',  'price': 120},
{ 'name': 'p7',  'price': 200}
],
'memory': [
{ 'name': '16GB',  'price': 75},
{ 'name': '32GB',  'price': 150}
],
'storage': [
{ 'name': '1TB',  'price': 50},
{ 'name': '2TB',  'price': 100}
]
}

my_p3 = components['processors'][0]

print(my_p3['name'] + ' ' + str(my_p3['price']))
instead of keeping separate lists, this combines both together, but there are still lists nested inside the dictionary, although the data is grouped together in a way which looks a bit more 'human' (using strings instead of numbers, and putting each component closer to its price).

The problem with writing separate lists for your prices and your component names is that you need to keep switching between those lists, which doesn't seem very intuitive to me. This way of grouping things might feel a bit more intuitive to you hopefully!

this is fantastic! thank you very much for your help!

Reply
Submit reply
Turn on thread page Beta

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: February 21, 2018
The home of Results and Clearing

### 3,132

people online now

### 1,567,000

students helped last year
Today on TSR

### A-level grade boundaries

Hang on, have Edexcel's come out already?

### University open days

1. SAE Institute
Animation, Audio, Film, Games, Music, Business, Web Further education
Thu, 16 Aug '18
2. Bournemouth University
Clearing Open Day Undergraduate
Fri, 17 Aug '18
3. University of Bolton
Undergraduate Open Day Undergraduate
Fri, 17 Aug '18
Poll
Useful resources

## Make your revision easier

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.