对与列表中的项目匹配的字典值执行操作?

发布于 2024-11-27 01:59:15 字数 458 浏览 4 评论 0原文

我有一个包含 46,000 个键:值对的字典,其中每个键都有一个 3 项列表作为值:

my dict = {key1: ['A', 'B', 'C'], key2: ['B', 'A', 'G'], key3: ['Z', 'H', 'I']......}

我有一个包含数百个值的列表:

L1 = ['A', 'A', 'B', 'D', ......]

如何迭代列表 L1 以及对于 L1 中的每个项目,匹配每个字典值,其中value[0] 与列表项匹配吗?然后,我希望仅对列表项与字典中的值[0]匹配的键:值对对字典的值[1]和值[2]执行其他操作。

在上面的示例中,L1 中的第一项 - 'A' 将仅匹配 key1: ['A', 'B', 'C']

我似乎无法找到一种方法来做到这一点?感谢您的帮助!

I have a dictionary with 46,000 key:value pairs where each key has a 3 item list as values:

my dict = {key1: ['A', 'B', 'C'], key2: ['B', 'A', 'G'], key3: ['Z', 'H', 'I']......}

I have a list with hundreds of values:

L1 = ['A', 'A', 'B', 'D', ......]

How do I iterate through the list L1 and for each item in L1, match each dictionary value where value[0] matches the list item? I then wish to perform other operations on value[1] and value[2] of the dictionary only on those key:value pairs where a list item matched value[0] in the dictionary.

In the above example, the first item in L1 - 'A' would match only key1: ['A', 'B', 'C'].

I can't seem to figure out a way to do this? Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇柔作态 2024-12-04 01:59:15

如果没有任何快捷方式,您可以编写如下内容:

#!/usr/bin/env python

d = {
    'key1' : ['A', 'B', 'C'], 
    'key2' : ['B', 'A', 'G'], 
    'key3' : ['Z', 'H', 'I']
}

l = ['A', 'A', 'B', 'D']

uniq_l = set(l) # you don't need to check duplicates twice

for key, value in d.items():
    if value[0] in uniq_l:
        print "Match", key, value

# Output:
# Match key2 ['B', 'A', 'G']
# Match key1 ['A', 'B', 'C']

Without any shortcuts, you could write something like this:

#!/usr/bin/env python

d = {
    'key1' : ['A', 'B', 'C'], 
    'key2' : ['B', 'A', 'G'], 
    'key3' : ['Z', 'H', 'I']
}

l = ['A', 'A', 'B', 'D']

uniq_l = set(l) # you don't need to check duplicates twice

for key, value in d.items():
    if value[0] in uniq_l:
        print "Match", key, value

# Output:
# Match key2 ['B', 'A', 'G']
# Match key1 ['A', 'B', 'C']
眼泪也成诗 2024-12-04 01:59:15

您写道:“我如何迭代列表 L1 ...” 所以我认为按此顺序执行操作对您很重要:

from collections import defaultdict

my_dict = {
    'key1': ['A', 'B', 'C'],
    'key2': ['B', 'A', 'G'],
    'key3': ['Z', 'H', 'I'],
    'key4': ['A', 'Q', 'W'],
}

L1 = ['A', 'A', 'B', 'D']

lookup = defaultdict(list)

for items in my_dict.itervalues():
    lookup[items[0]].append(items[1:])

for key in L1:
    for items in lookup[key]:
        print items

给出:

['B', 'C']
['Q', 'W']
['B', 'C']
['Q', 'W']
['A', 'G']

Python 2.7,顺便说一句。

You wrote: "How do I iterate through the list L1 ..." so I assume doing things in this order is important to you:

from collections import defaultdict

my_dict = {
    'key1': ['A', 'B', 'C'],
    'key2': ['B', 'A', 'G'],
    'key3': ['Z', 'H', 'I'],
    'key4': ['A', 'Q', 'W'],
}

L1 = ['A', 'A', 'B', 'D']

lookup = defaultdict(list)

for items in my_dict.itervalues():
    lookup[items[0]].append(items[1:])

for key in L1:
    for items in lookup[key]:
        print items

gives:

['B', 'C']
['Q', 'W']
['B', 'C']
['Q', 'W']
['A', 'G']

Python 2.7, BTW.

指尖凝香 2024-12-04 01:59:15
>>> my_dict
{'key3': ['Z', 'H', 'I'], 'key2': ['B', 'A', 'G'], 'key1': ['A', 'B', 'C']}
>>> L1 
['A', 'A', 'B', 'D']
>>> {i: {key: value for key, value in my_dict.iteritems() if value[0] == i} for i in set(L1)}
{'A': {'key1': ['A', 'B', 'C']}, 'B': {'key2': ['B', 'A', 'G']}, 'D': {}}

这意味着 'A' 匹配 {'key1': ['A', 'B', 'C']} 等。它是一个字典,因为在 46000 个值中,我认为可能有多个匹配项。

>>> my_dict
{'key3': ['Z', 'H', 'I'], 'key2': ['B', 'A', 'G'], 'key1': ['A', 'B', 'C']}
>>> L1 
['A', 'A', 'B', 'D']
>>> {i: {key: value for key, value in my_dict.iteritems() if value[0] == i} for i in set(L1)}
{'A': {'key1': ['A', 'B', 'C']}, 'B': {'key2': ['B', 'A', 'G']}, 'D': {}}

It means 'A' matches {'key1': ['A', 'B', 'C']} etc. It is a dict because of in 46000 values, i thought there could be more than one matches.

涙—继续流 2024-12-04 01:59:15

我可能会构建一个辅助 dict

>>> mydict = {'key1': ['A', 'B', 'C'], 'key2': ['B', 'A', 'G'], 'key3': ['Z', 'H', 'I']}
>>> from collections import defaultdict>>> mydict_aux = defaultdict(list)
>>> for k,v in mydict.items():
...     mydict_aux[v[0]].append(k)
... 
>>> mydict_aux['A']
['key1']

I would probably build an auxilary dict

>>> mydict = {'key1': ['A', 'B', 'C'], 'key2': ['B', 'A', 'G'], 'key3': ['Z', 'H', 'I']}
>>> from collections import defaultdict>>> mydict_aux = defaultdict(list)
>>> for k,v in mydict.items():
...     mydict_aux[v[0]].append(k)
... 
>>> mydict_aux['A']
['key1']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文