简体   繁体   English

将字典中的键组合成列表中的元组

[英]Combining Keys in Dictionary into tuples in a list

I'm looking for a solution to search keys in a dictionary according to a list as value, and later append the keys to a list of tuples. 我正在寻找一种解决方案,以根据列表作为值搜索字典中的键,然后将键附加到元组列表中。 I'm able to search for the correct keys but can't find a way to build the list I'm expecting. 我能够搜索正确的键,但是找不到建立期望的列表的方法。 Appreciate for kind help. 感谢您的帮助。

As below, I'd like to find all keys in d which has a value equals to the element in the list l , and further put all searched keys into a list of tuples as shown in expected output. 如下所示,我想找到d中所有值等于列表l的元素的所有键,然后将所有搜索到的键放入元组列表中,如预期输出所示。

d = {'acutrar': 'acutrar',
 'aguosa': 'aguoso',
 'capitalizareis': 'capitalizar',
 'conocerán': 'conocer',
 'conociéremos': 'conocer',
 'conocían': 'conocer',
 'conocías': 'conocer',
 'conozcas': 'conocer',
 'pales': 'palar',
 'planeareis': 'planear',
 'planearás': 'planear',
 'planeasteis': 'planear',
 'planeáramos': 'planear'}

l = ['conocer', 'NOT FOUND', 'NOT FOUND', 'planear']

for word in l:
    for (x,y) in d.items():
        if y == word:
            print(word, x) #I can only search for the keys but don't know how to build that list of tuples

Expected Output: 预期产量:

[('conocerán','conocías','conozcas','conocían','conociéremos'),('NOT FOUND'),('NOT FOUND'),('planeáramos','planeareis','planearás','planeasteis')]

Why don't transform the dictionary into a more suitable/convenient structure and perform the O(1) lookups by key instead of looping over all of the items and searching for the value: 为什么不将字典转换成更合适/更方便的结构并通过键执行O(1)查找,而不是遍历所有项目并搜索值:

from collections import defaultdict

d = {'acutrar': 'acutrar',
 'aguosa': 'aguoso',
 'capitalizareis': 'capitalizar',
 'conocerán': 'conocer',
 'conociéremos': 'conocer',
 'conocían': 'conocer',
 'conocías': 'conocer',
 'conozcas': 'conocer',
 'pales': 'palar',
 'planeareis': 'planear',
 'planearás': 'planear',
 'planeasteis': 'planear',
 'planeáramos': 'planear'}


t = defaultdict(list)
for key, value in d.items():
    t[value].append(key)

l = ['conocer', 'NOT FOUND', 'NOT FOUND', 'planear']

print([t.get(item, [item]) for item in l])

Prints: 印刷品:

[
    ['conozcas', 'conociéremos', 'conocías', 'conocerán', 'conocían'], 
    ['NOT FOUND'], 
    ['NOT FOUND'], 
    ['planeáramos', 'planearás', 'planeareis', 'planeasteis']
]

Your result is surprisingly a blend of keys and values. 您的结果令人惊讶地是键和值的混合。

A more homogeneous solution can be obtained by 可以通过以下方法获得更均匀的溶液

[tuple(k for (k,v) in d.items() if v == w) for w in l]  

for 对于

[('conocías', 'conozcas', 'conociéremos', 'conocerán', 'conocían'),
 (),
 (),
 ('planeareis', 'planeáramos', 'planeasteis', 'planearás')]

or : 要么 :

{w:tuple(k for (k,v) in d.items() if v == w) for w in l}

for 对于

{'conocer': ('conocías', 'conozcas', 'conociéremos', 'conocerán', 'conocían'),
 'NOT FOUND': (),
 'planear': ('planeareis', 'planeáramos', 'planeasteis', 'planearás')}

To get your expected output, you can build the list of tuples like this: 为了获得预期的输出,您可以构建如下的元组列表:

for word in l:
    hits = tuple()
    for (x,y) in d.items():
        if y == word:
            hits += (x,)
    if not hits:
        hits = ('NOT FOUND',)
    results.append(hits)

print(results)

First no need to iterate on the list of searched values IMO. 首先,无需在IMO搜索值列表中进行迭代。

You could just do: 您可以这样做:

final = []

for k, v in d.items():
    if v in l:
        final.append(k)
final = tuple(final)

But you could even simplify it via a list comprehension as below: 但是您甚至可以通过以下列表理解来简化它:

final = tuple(k for k, v in d.items() if v in l)

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

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