是这样的,我有一个列表,里边元素是若干字典.
提示用户输入一个数字,怎么判断用户输入的这个数字是否是我列表里边某一个字典的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小白,请各位指点一二
# -*- 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('不存在')
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('不存在')