python之字典查找键、值

每日一记python之字典查找键、值
今天学习了字典查找键、值的方法。

#新建字典实例
d = {
    
    2 : 4, 5 : 7, 8 : 10, 11 : 13}
#判断字典中key
if 2 in d:
	print('yes1')
#判断字典中values
if 4 in d.values():
	print('yes2')

执行结果

yes1
yes2
#dict.values()返回字典中所有value
#类似的dict.keys()返回字典中所有key

猜你喜欢

转载自blog.csdn.net/qq_45847100/article/details/120642410