简体   繁体   English

使用列表作为python中的值在字典中搜索特定列表

[英]Searching specific list within dictionary with lists as values in python

I have a dictionary with strings as keys and lists as values.我有一个以字符串为键、以列表为值的字典。 I now have another list (list2) and I would like to search if there are any dictionary values lists that contain all the items of list2.我现在有另一个列表 (list2),我想搜索是否有任何包含 list2 的所有项目的字典值列表。 I would then like the output to be the key of the matching dictionary value list.然后我希望输出是匹配字典值列表的键。

My code now looks something like this:我的代码现在看起来像这样:

for elem in list2:
    for elem2 in dict.keys():
        if all(item in elem2 foritem in list2):
            print(elem2)

value_found_list = dict.get(elem2)

print(value_found_list)

Edit minimal reproducable example:编辑最小的可重现示例:

dict{
"key1": ['item1', 'item2', 'item3'], 
"key2": ['item2', 'item4', 'item5'],
"key3": ['item3', 'item6', 'item4']
}
list = ['item2', 'item3']

How would I with this data return which dictionary key contains all items in the list?我将如何使用此数据返回哪个字典键包含列表中的所有项目?

You're better off changing your second list to a set if you're planning on doing membership checking.如果您打算进行成员资格检查,最好将第二个list更改为一set

Here's a short function that does just that.这是一个可以做到这一点的简短函数。

Edit: Since you're doing it the other way around it's probably less time intensive to just use the list in your dict .编辑:由于您以相反的方式进行操作,因此仅使用dict的列表可能不会占用太多时间。 However, It may make more sense to have them in the form of a set in the first place.但是,首先将它们以set的形式存在可能更有意义。 A set is hashed which translates to faster lookup time.一个set被散列,这意味着更快的查找时间。 If your list s are pretty long, you'll save some computation time by having them in this form.如果您的list很长,您可以通过将它们采用这种形式来节省一些计算时间。

def get_key(dct, lst2):
    # iterate through every key and list
    for key, lst in dct.items():
        # if all the list items are present in your second list
        if all(i in lst for i in lst2):
            return key
    # default to returning None
    return None

dct = {
"key1": ['item1', 'item2', 'item3'], 
"key2": ['item2', 'item4', 'item5'],
"key3": ['item3', 'item6', 'item4']
}
lst = ['item2', 'item3']
print(get_key(dct, lst))

Where you were going wrong is here.你出错的地方在这里。

    for elem2 in dict.keys():
        if all(item in elem2 foritem in list2):

elem2 is a string key. elem2是一个字符串键。 item in elem2 is looking for the full string within the key. item in elem2正在寻找键中的完整字符串。 dict.values() returns the values or lists that you were probably looking for. dict.values()返回您可能正在寻找的值或列表。

However dict.items() returns the keys and values as a tuple which is probably what you need.但是dict.items()将键和值作为元组返回,这可能是您需要的。

Watch this看这个

a = {'one':[1, 2, 3, 4], 'two':[5, 6, 7, 8], 'three':[9, 10]}
b = {'four':[1, 2, 3, 4], 'five':[6, 7, 8], 'six':[9, 10]}
for akey in a:
    for bkey in b:
        if a[akey] == b[bkey]:
            print(akey, bkey)

outputs:输出:

one four
three six
l=[i for i in your_dict if set(your_dict[i]).union(set(list2))==set(your_dict[i])]

print(l)

for:为了:

your_dict={
"key1": ['item1', 'item2', 'item3'], 
"key2": ['item2', 'item4', 'item5'],
"key3": ['item3', 'item6', 'item4']
}
list2 = ['item2', 'item3']

The output is:输出是:

['key1']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM