栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在嵌套的Python词典中搜索键

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在嵌套的Python词典中搜索键

你近了

idnum = 11# The loop and 'if' are good# You just had the 'break' in the wrong placefor id, idnumber in A.iteritems():    if idnum in idnumber.keys(): # you can skip '.keys()', it's the default       calculate = some_function_of(idnumber[idnum])       break # if we find it we're done looking - leave the loop    # otherwise we continue to the next dictionaryelse:    # this is the for loop's 'else' clause    # if we don't find it at all, we end up here    # because we never broke out of the loop    calculate = your_default_value    # or whatever you want to do if you don't find it

如果您需要知道

11
内部
dict
s中有多少个作为键,则可以:

idnum = 11print sum(idnum in idnumber for idnumber in A.itervalues())

之所以可行,是因为每个密钥只能进入

dict
一次,因此您只需测试密钥是否退出即可。
in
返回
True
False
等于
1
0
,因此
sum
是的出现次数
idnum



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号