检查列表中的项目是否存在于字典中 [英] Check if items in a list exist in dictionary
问题描述
dict_a = {'one':{'bird':2,'tree ':6,'sky':1,'TOTAL':9},
'two':{'apple':3,'sky':1,'TOTAL':4},
' 3':{'tree':6,'TOTAL':6},
'four':{'nada':1,'TOTAL':1},
'five' ':2,'bird':3,'TOTAL':5}
}
和列表:
list1 = ['bird','tree']
newlist = []
如何检查list1中的项目是否在dict_a的嵌套字典中并将其附加到新列表?输出应如下所示:
newlist = ['one','three','five']
因为鸟和树恰好是一个,三个和五个的嵌套字典。
我可以想到的是:
for s,v in dict_a.items():
for s1,v1 in v.items():
for list in list1:
if item == s1:
newlist.append(s)
使 list1
并使用字典视图和列表解析:
set1 = set(list1)
newlist = [key for key,value in dict_a.iteritems()if value。 viewkeys()& set1]
在Python 3中,使用 value.keys()
和 dict_a.items
。
这将测试字典键之间是否有一个交集和您正在寻找的一组密钥(一个有效的操作)。
演示:
>>> dict_a = {'one':{'bird':2,'tree':6,'sky':1,'TOTAL':9},
...'two':{'apple' ,'sky':1,'TOTAL':4},
...'three':{'tree':6,'TOTAL':6},
...'four' {'nada':1,'TOTAL':1},
...'five':{'orange':2,'bird':3,'TOTAL':5}
.. 。}
>>> set1 = {'bird','tree'}
>>>> [key for key,value in dict_a.iteritems()if value.viewkeys()& set1]
['three','five','one']
请注意字典排序是任意的(取决于使用的键和字典插入和删除历史),所以输出列表顺序可能不同。
从技术上讲,您可以直接使用您的列表( value.viewkeys()& list1
的作品),但使它成为一个更清楚的目的。
My question might be a little complicated to understand but here's actually the thing. I have a nested dictionary that looks like this:
dict_a = {'one': {'bird':2, 'tree':6, 'sky':1, 'TOTAL':9},
'two': {'apple':3, 'sky':1, 'TOTAL':4},
'three': {'tree':6, 'TOTAL':6},
'four': {'nada':1, 'TOTAL':1},
'five': {'orange':2, 'bird':3, 'TOTAL':5}
}
and a list:
list1 = ['bird','tree']
newlist = []
how can I check the items in list1 whether it is in the nested dictionary of dict_a and append it to the newlist? The output should look like this:
newlist = ['one','three','five']
since bird and tree happened to be in the nested dictionary of one, three and five.
What I can think of is:
for s,v in dict_a.items():
for s1,v1 in v.items():
for item in list1:
if item == s1:
newlist.append(s)
Make list1
a set and use dictionary views, and a list comprehension:
set1 = set(list1)
newlist = [key for key, value in dict_a.iteritems() if value.viewkeys() & set1]
In Python 3, use value.keys()
and dict_a.items
instead.
This tests if there is a set intersection between the dictionary keys and the set of keys you are looking for (an efficient operation).
Demo:
>>> dict_a = {'one': {'bird':2, 'tree':6, 'sky':1, 'TOTAL':9},
... 'two': {'apple':3, 'sky':1, 'TOTAL':4},
... 'three': {'tree':6, 'TOTAL':6},
... 'four': {'nada':1, 'TOTAL':1},
... 'five': {'orange':2, 'bird':3, 'TOTAL':5}
... }
>>> set1 = {'bird','tree'}
>>> [key for key, value in dict_a.iteritems() if value.viewkeys() & set1]
['three', 'five', 'one']
Note that dictionary ordering is arbitrary (depending on the keys used and dictionary insertion and deletion history), so the output list order may differ.
Technically speaking, you can use your list directly too (value.viewkeys() & list1
works) but making it a set states your intention more clearly.
这篇关于检查列表中的项目是否存在于字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!