首页 新闻 搜索 专区 学院

python中怎么判断用户输入的数字是否在一个字典列表的value

0
悬赏园豆:50 [已解决问题] 解决于 2018-07-08 14:58

是这样的,我有一个列表,里边元素是若干字典.

  提示用户输入一个数字,怎么判断用户输入的这个数字是否是我列表里边某一个字典的value值

列表各个字典的key相同,就相当于这样:

user = [
{"name":"Yang","weight":55,"height":172,"ID":1},{"name":"Liu","weight":54,"height":170,"ID":2},{"name":"Pan","weight":52,"height":170,"ID":3}
]
# 这里的ID是顺序增加的 Query = int(input("输入一个数字:")) # 这里判断Query是否等于user列表字典的某一个字典的ID值

python小白,请各位指点一二

清风吹杨柳的主页 清风吹杨柳 | 初学一级 | 园豆:24
提问于:2018-07-07 11:01
< >
分享
最佳答案
1
# -*- coding:utf-8 -*-

user = [
{"name":"Yang","weight":55,"height":172,"ID":1},
{"name":"Liu","weight":54,"height":171,"ID":2},
{"name":"Pan","weight":52,"height":170,"ID":3}
]

# 这里的ID是顺序增加的
Query = int(input("输入一个数字:"))

# 这里判断Query是否等于user列表字典的某一个字典的ID值
Flag = False
for item in user:
    if Query == item['ID']:
        Flag = True
        break

if(Flag):
    print('存在')
else:   
    print('不存在')
收获园豆:25
silence-cc | 初学一级 |园豆:138 | 2018-07-07 14:36
其他回答(1)
1
user = [{
    "name": "Yang",
    "weight": 55,
    "height": 172,
    "ID": 1
}, {
    "name": "Liu",
    "weight": 54,
    "height": 170,
    "ID": 2
}, {
    "name": "Pan",
    "weight": 52,
    "height": 170,
    "ID": 3
}]
# 这里的ID是顺序增加的
Query = int(input("输入一个数字:"))
# 这里判断Query是否等于user列表字典的某一个字典的ID值

result = filter(lambda x: x['ID'] == Query, user)
isExists = list(result)
if len(isExists) > 0:
    print('存在')
else:
    print('不存在')
收获园豆:25
悟行 | 园豆:12429 (专家六级) | 2018-07-07 12:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册