Hey there! Sign in to join this conversationNew here? Join for free
x Turn on thread page Beta

help with 2 arrays watch

Announcements
    • Thread Starter
    Offline

    5
    ReputationRep:
    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!
    Offline

    17
    ReputationRep:
    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/
    • Thread Starter
    Offline

    5
    ReputationRep:
    (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
    • Thread Starter
    Offline

    5
    ReputationRep:
    Name:  imagepython.png
Views: 11
Size:  96.7 KBI 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
    Offline

    17
    ReputationRep:
    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!
    • Thread Starter
    Offline

    5
    ReputationRep:
    (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
Updated: February 21, 2018
Poll
Do you agree with the proposed ban on plastic straws and cotton buds?

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.