简体   繁体   English

如何从两个单独的列表中获取两个值以进行打印(Python)

[英]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包含人的名字,列表b包含人的电话号码:

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 . 我需要做的是提示用户输入将是列表a的名称的输入,然后python应该自动从列表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”,它应该提供输出:

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: 如果您只想使用列表来执行此操作,则可以使用index但是请注意,这将导致性能下降:

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 : 如果您不能使用phone = dict(zip(a,b)).get("peter")则可以使用列表推导

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

It returns a list of "peter" 's phone numbers. 它返回"peter"的电话号码列表。 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) ). 如果列表很大,则它比dict方法慢得多( O(1) vs. O(N) )。

If you need only one number then using next() : 如果只需要一个数字,则使用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. 如果列表中没有这样的名称,则phone为“ None

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? 您不能只使用if语句吗?

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 但是你应该告诉老师使用dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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