python检查字典是否在列表中

标签 python python-3.x list dictionary

我有一个名为 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.')

但似乎不能正常工作。

最佳答案

试试这个

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

关于python检查字典是否在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025765/

相关文章:

python - 访问 murano 仪表板时 openstack newton keystone 出现 "No module named memcache"错误

python - 模型类在 django 中被视为局部变量

Python 3 - 从低于 1920x1080 的文件夹中删除图片

python - 动态扩展 Django 表单

python - 如何在 Python 3 urllib 中设置任意主机 header ?

python - 有没有更好的方法在 python 中执行 "unravel"函数?

python 'list indices must be integers, not tuple"

python 拆分列表中的数字

java - 从一组聚合(总和、平均值等)构建具体列表

python - AWS Lambda RDS MySQL 数据库连接接口(interface)错误