简体   繁体   English

在有序字典列表中搜索列表

[英]searching for a list in a list of ordered dictionaries

So I have a list of ordered dictionaries which all have 'name' keys which take a string as a value and then a series of other keys which take integers as values.所以我有一个有序字典的列表,它们都有'name'键,它们以字符串为值,然后是一系列其他以整数作为值的键。 I also have a list of integers separate from the list of ordered dictionaries.我还有一个与有序字典列表分开的整数列表。 I would like to search through the list of ordered dictionaries and see if there are any dictionaries which have all the integers in the list, and if so, what the 'name' value in that list is.我想搜索有序词典列表,看看是否有任何词典包含列表中的所有整数,如果有,该列表中的“名称”值是什么。 Is there any way to do this?有没有办法做到这一点?

Ie i have a list of dictionaries with dictionaries like so:即我有一个字典列表,其中的字典如下:

dict = OrderedDict({('name' : 'John'), ('int1': 5), ('int2': 3), ('int3': 1)}), OrderedDict({('name': 'Jack'), ('int1': 1), ('int2': 6), ('int3': 7)}) 

and then a list of integers like: list = [3, 2, 5]然后是一个整数列表,如: list = [3, 2, 5]

and if there is a match between the list and the integers in any of the ordered dictionaries, I would like to get the name returned (so in the above case, John).如果列表和任何有序字典中的整数之间存在匹配,我想得到返回的名称(所以在上面的例子中,约翰)。

This may be very basic in which case I apologise, I'm very new to python and coding in general.这可能是非常基本的,在这种情况下我很抱歉,我对 python 和一般编码非常陌生。 I've been searching for hours but I haven't found anything I can understand.我一直在寻找几个小时,但我没有找到任何我能理解的东西。

If I understand your question right (not that the example data, or the result for John is correct), you may be looking for如果我正确理解您的问题(不是示例数据或 John 的结果是正确的),您可能正在寻找

dicts = [
    {"name": "John", "int1": 5, "int2": 3, "int3": 1},
    {"name": "Jack", "int1": 1, "int2": 6, "int3": 7},
    {"name": "Mallory", "int1": 1, "int2": 6, "int3": 3},
]


def find_with_keyset(dicts, keyset):
    for dict in dicts:
        if all(
            key in dict and dict[key] == value
            for (key, value) in keyset.items()
        ):
            yield dict


def find_first_with_keyset(dicts, keyset):
    for result in find_with_keyset(dicts, keyset):
        return result


for match in find_with_keyset(dicts, {"int2": 6}):
    print(match["name"])

print("---")

print(find_first_with_keyset(dicts, {"int1": 5, "int2": 3, "int3": 1}))

This prints out这打印出来

Jack
Mallory
---
{'name': 'John', 'int1': 5, 'int2': 3, 'int3': 1}

The idea is that the find_with_keyset generator function filters a given iterable of dicts based on a key subset;这个想法是find_with_keyset生成器 function 基于键子集过滤给定的 dicts 迭代; for ease-of-use there's find_first_with_keyset which will return the first match, or None.为了易于使用, find_first_with_keyset将返回第一个匹配项,或 None。

To turn [1, 2, 3] to {'int1': 1, ...} you can use eg {f'key{i}': value for (i, value) in enumerate([1, 2, 3], 1)} .要将[1, 2, 3]转换为{'int1': 1, ...}您可以使用例如{f'key{i}': value for (i, value) in enumerate([1, 2, 3], 1)}

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

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