对与列表中的项目匹配的字典值执行操作?
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果没有任何快捷方式,您可以编写如下内容:
Without any shortcuts, you could write something like this:
您写道:“我如何迭代列表 L1 ...” 所以我认为按此顺序执行操作对您很重要:
给出:
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:
gives:
Python 2.7, BTW.
这意味着 'A' 匹配 {'key1': ['A', 'B', 'C']} 等。它是一个字典,因为在 46000 个值中,我认为可能有多个匹配项。
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.
我可能会构建一个辅助
dict
I would probably build an auxilary
dict