简体   繁体   中英

How to get two values from two seperate lists to print (python)

Here's my problem, I have two different lists, list a which contains the name of people and list b which contains their phone numbers:

a = ["peter", "bob", "john", "jack"]
b = ["8954 3434", "8999 4432", "8976 5443", "8990 3331"]

What I need to do is prompt users for a input which will be a name from list a and then python should automatically print out the phone number for that person from list b .

I came across a code that seems pretty interesting:

for x, y in zip(a, b):
    print x, y

This pretty much prints every single name and the corresponding phone number next to it which is basically what I need, however rather than every name from list a appearing, what I need is for the user to be able to enter a single name and for that names phone number to show up.

Just to clarify, using the above code gives me the output:

peter 8954 3434
bob 8999 4432
john 8976 5443
jack 8990 3331

While what I want is for the user to be able to enter a name eg "peter" which should give the output:

peter 8954 3434

Is there a way to edit that above code to get things to work properly? Thanks for any help.

Put your data in a dictionary:

>>> a = ["peter", "bob", "john", "jack"]
>>> b = ["8954 3434", "8999 4432", "8976 5443", "8990 3331"]
>>> phone_numbers = dict(zip(a,b))

Then you can get someone's phone number from their name:

>>> phone_numbers['john']
'8976 5443'

If you want to do this only using lists, you can use index but note that this will have poor performance:

b[a.index('john')]
'8976 5443'

Do you want something like

print b[a.index("bob")]

?

The most basic way to solve this:

who = raw_input("Give a name")
for name, number in zip(a, b): 
    if name == who:
        print name, number

I hope it's homework.

I have to use a list for this problem

If you can't use phone = dict(zip(a,b)).get("peter") then using list comprehensions :

phones = [phone for name, phone in zip(a,b) if name == "peter"]

It returns a list of "peter" 's phone numbers. It allows you to have several entries with the same name. If lists are large then it is much slower then the dict approach ( O(1) vs. O(N) ).

If you need only one number then using next() :

phone = next((phone for name, phone in zip(a,b) if name == "peter"), None)

It returns the first match from the list. phone is None if there is no such name in the list.

Put them into a dictionary:

people = dict(zip(a, b))

Then just look names up:

name = "john"
print("%s's number is %s" % (input, people[name]))

Create a dictionary of the zipped lists:

names = ["peter", "bob", "john", "jack"]
phones = ["8954 3434", "8999 4432", "8976 5443", "8990 3331"]
phones_by_name = dict(zip(names, phones))
print "bob", phones_by_name["bob"]
print "jack", phones_by_name["jack"]

gives:

bob 8999 4432
jack 8990 3331

Can't you just use an if statement?

for x, y in zip(a, b):
    if x == name_input:
        print x, y

or I guess in the context of your code

for name, phone in zip(names_list, phone_list):
    if name == name_input:
        print name, phone

But you should tell your teacher to use dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM