简体   繁体   English

如何使用我想要获取的字典的一个键/值对从字典列表中访问字典

[英]How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch

I have a list of dictionaries, which all have the same keys.我有一个字典列表,它们都有相同的键。 I have a specific value of one key and want to access/print the dictionary, which contains this specific value.我有一个键的特定值,想访问/打印包含此特定值的字典。 I couldn't think of any way, other than looping around the whole list, checking the corresponding value of the key and printing it out using if statement, that is if the given value matched the key.除了遍历整个列表,检查键的相应值并使用if语句打印出来之外,我想不出任何方法,即给定的值是否与键匹配。

for enrollment in enrollments:
    if enrollment['account_key'] == a:
        print(enrollment)
    else:
        continue

This does not really seem the most efficient way to handle the task.这似乎并不是处理任务的最有效方式。 What would be a better solution?什么是更好的解决方案?

Some options:一些选项:

1- Use the loop like you do here, although this could be written simpler without the continue. 1- 像这里一样使用循环,尽管如果没有 continue,这可以写得更简单。

for enrollment in enrollments:
    if enrollment['account_key'] == a:
        print(enrollment)

2- Use a generator expression and next 2-使用生成器表达式和next

enrollment = next(e for e in enrollments if e['account_key'] == a)
print(enrollment)

3- Convert the list of dictionaries into a dictionary of dictionaries. 3- 将字典列表转换为字典字典。 This is a good option if you have to do this operation many times and there is only one value for each account_key如果您必须多次执行此操作并且每个account_key只有一个值,这是一个不错的选择

accounts = {
    enrollment['account_key']: enrollment
    for enrollment in enrollments
}
print(accounts[a])

4- Same as above, but if there are multiple values for the same key, you can use a dict of lists of dicts. 4- 同上,但如果同一个键有多个值,您可以使用字典列表。

accounts = defaultdict(list)
for enrollment in enrollments:
    accounts[enrollment['account_key']].append(enrollment)

for enrollment in accounts[a]:
    print(enrollment)

You can use a comprehension (iterator) to get the subset of dictionaries that match your criteria.您可以使用理解(迭代器)来获取符合您的条件的字典子集。 In any case this will be a sequential search process.无论如何,这将是一个顺序搜索过程。

enrolments = [ {'account_key':1, 'other':99},
               {'account_key':2, 'other':98},
               {'account_key':1, 'other':97},
               {'account_key':1, 'other':96},
               {'account_key':3, 'other':95} ]

a = 1
found = (d for d in enrolments if d['account_key']==a)
print(*found,sep="\n")

{'account_key': 1, 'other': 99}
{'account_key': 1, 'other': 97}
{'account_key': 1, 'other': 96}

暂无
暂无

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

相关问题 如何从包含键作为字典列表的字典中获取第一个键值对(即“类型”:45) - How do i fetch first key value pair (i.e., 'type': 45) from a dictionary which contains key as list with dictionaries 如何使用单个键:值对从字典项列表中提取一项? - How can I pull one item, from a list of dictionary items, using a single key:value pair? 如何使用内部键:值对将字典列表转换为字典字典 - How to convert list of dictionaries into a dictionary of dictionaries by using inner key:value pair 如何将键/值对插入词典列表中包含的每个词典中 - How can I insert a key/value pair into each dictionary contained in a list of dictionaries 如何获取仅包含一个项目的字典的键/值对? - How to fetch the key/value pair of a dictionary only containing one item? 将带有字典的字典转换为具有相同键值对的列表 - convert dictionaries with dictionary into list with same key value pair 根据键值对更新字典列表中的整个字典 - update an entire dictionary in a list of dictionaries based on key value pair 如果与列表中每个字典中的一个键的值匹配,则将具有不同词典值的新键添加到词典列表中 - Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 如何从基于另一个键值对的列表中的字典中访问值? - How to access values from a dictionary inside a list based on another key-value pair?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM