python检查字典是否在列表中

问题描述 投票:2回答:6

我有一个名为clients_list的列表,其中包含以下字典:

    clients_list =
    [
            {'John Guy': [28, '03171992', 'Student']},
            {'Bobby Jones': [22, '02181982', 'Student']},
            {'Claire Eubanks': [18, '06291998', 'Student']},
    ]

如何使用输入的答案检查是否有人在此列表中?我试过了代码

elif answer in clients_list:
    print(f'{answer} is in our database.')

但它似乎没有正常工作。

python python-3.x list dictionary
6个回答
2
投票

试试这个

clients_list = [{'John Guy': [28, '03171992', 'Student']},
            {'Bobby Jones': [22, '02181982', 'Student']},
            {'Claire Eubanks': [18, '06291998', 'Student']}]

c= 'John Guy'

for item in clients_list:
    if c in item:
        print c + 'is in our database.'
        break

1
投票

假设answer包含"John Guy"。然后这个测试if answer in clients_list询问字符串"John Guy"是否在字典列表中,当然它不是,因为clients_list是字典列表,而不是字符串。现在你明白为什么你的测试不符合你的期望吗?

这证明了juanpa.arrivilaga的观点,即数据结构与你正在使用的数据结构并不完全匹配。如果要对名称进行查找,则这些名称应为字典键。就像是

clients_list = {
        'John Guy': [28, '03171992', 'Student'],
        'Bobby Jones': [22, '02181982', 'Student'],
        'Claire Eubanks': [18, '06291998', 'Student'],
        }

您还可以考虑将字典值命名为元组而不是列表。


0
投票

列表元素之间的逗号。

clients_list =[{'John Guy': [28, '03171992', 'Student']},
            {'Bobby Jones': [22, '02181982', 'Student']},
            {'Claire Eubanks': [18, '06291998', 'Student']}]

至于问题,这应该有效

for d in clients_list:
    for person in d.items():
        if "John Guy" in person[0]:
            print (person[1])
            print (person[0]+" is in our database")

0
投票

如果你想匹配键,你可以使用set().union

clients_list = [{'John Guy': [28, '03171992', 'Student']},
                {'Bobby Jones': [22, '02181982', 'Student']},
                {'Claire Eubanks': [18, '06291998', 'Student']}]

x = input('Enter a name:')

if x in set().union(*clients_list):
    print('Name found')
else:
    print('Name not found')

0
投票

你可以尝试使用zip函数:

clients_list = [{'John Guy': [28, '03171992', 'Student']}, {'Bobby Jones': [22, '02181982', 'Student']}, {'Claire Eubanks': [18, '06291998', 'Student']}]

name = "John Guy"
if name in list(zip(*clients_list))[0]:
     print(name + " is in database.")

输出:

John Guy is in database.

list(zip(*clients_list))将返回一个包含元组的列表,其名称如下:

[('John Guy', 'Bobby Jones', 'Claire Eubanks')]

然后,您所做的就是使用[0]获取列表中的那个元组,并检查该元组是否存在您输入的名称。

或者,您可以解压缩单个元组名称:

names, = list(zip(*clients_list))

并用它来检查你的名字是否在那里:

if name in names:
     print(name + " is in database.")

0
投票
clients_list = [
            {'John Guy': [28, '03171992', 'Student']},
            {'Bobby Jones': [22, '02181982', 'Student']},
            {'Claire Eubanks': [18, '06291998', 'Student']}]

def get_name(answer):
    data = ("No Data Present in DB.",{'Error':'Not Found'})
    for index, val in enumerate(clients_list):
        if answer in val.keys():
            data =  (f'{answer} present in database & found at {index}', clients_list[index])
    return data

asnwer = input("Enter a Name")

found, value = get_name(asnwer)

print(found, value)
>>>Bobby Jones present in database & found at 1 {'Bobby Jones': [22, '02181982', 'Student']}
© www.soinside.com 2019 - 2024. All rights reserved.