简体   繁体   English

在Python词典中如何找到基于第一个值的键?

[英]In Python dictionary how to find a based on first value of the key?

I am working with python dictionary to store the key value pair. 我正在使用python字典来存储键值对。 I have duplicate keys such as for key 6 there are to values [4,2], [5,1] , which i can not store in the dictionary since it doesn't allow duplicate keys. 我有重复的键,例如键6的值[4,2], [5,1] ,由于不允许重复的键,我无法将其存储在字典中。

Therefore, I came up with indexing the key itself eg for key 7 there are 3 values so I stored them with index as: 因此,我想出了索引键本身的索引,例如对于键7,有3个值,所以我将它们存储为index:

{(7, 0): [6, 1], (7, 1): [5, 2], (7, 2): [4, 3]}

Based on above logic I generated the following dictionary: 基于以上逻辑,我生成了以下字典:

{
(7, 0): [6, 1], 
(7, 1): [5, 2], 
(7, 2): [4, 3],

(6, 1): [4, 2], 
(6, 0): [5, 1], 

(5, 0): [4, 1], 
(5, 1): [3, 2], 

(3, 0): [2, 1], 

(4, 0): [3, 1]
}

My question is that how to find a set of values for a given key eg for key 7 I want to get the result as [6,1], [5,2], [4,3] . 我的问题是如何找到给定键的一组值,例如键7,我想得到的结果为[6,1], [5,2], [4,3]

PS: Only python2.7 standard libraries should be used for the solution. PS:该解决方案仅应使用python2.7标准库。

Thanks for your help. 谢谢你的帮助。

Your approach for creating a dict is not Good. 您创建字典的方法不好。 Instead of doing this, you should create a dict which holds values of all your items as list . 而不是这样做,您应该创建一个dict,将所有项目的值保存为list

Hence, your dictionary structure should be like: 因此,您的字典结构应类似于:

my_dict = {
    7: [[6, 1], [5, 2], [4, 3]],
...
}

Then you can simply access all the values of 7 with: 然后,您可以使用以下命令简单地访问7所有值:

my_dict[7]

Take a look at collections.defaultdict . 看一下collections.defaultdict


If you still want to go with your approach, you may use list comprehension to fetch the values as: 如果您仍然想使用该方法,则可以使用列表推导来获取以下值:

key = 7

[v for k , v in my_dict.items() if k[0]==key]
#                                   ^  matches 0th index of key and returns
#                                      returns all the values as list     

For your answer: 为您的答案:

print([v for k,v in d.items() if k[0]==7])

result: 结果:

[[6, 1], [5, 2], [4, 3]]

but you're not taking advantage of the dictionary. 但您没有利用字典。 For your task I would suggest collections.defaultdict with list as default argument 对于您的任务,我建议使用list作为默认参数的collections.defaultdict

Demo: 演示:

import collections

d = collections.defaultdict(list)

d[7].append([6,1])
d[7].append([5,2])
d[7].append([4,3])

print(d[7])

result: 结果:

[[6, 1], [5, 2], [4, 3]]

I would actually change the structure of your dictionary a bit to make it a dictionary that had numbers for keys (ie, your 6 and 7 that you're talking about here) and then list of tuples for the values. 实际上,我会稍微改变字典的结构,使其成为具有键数字的字典(例如,您在这里谈论的6和7),然后是值的元组列表。 Like this: 像这样:

{7: [(6, 1), (5, 2), (4, 3)], 6: ... }

You could also have a list of lists if you wanted. 如果需要,您也可以有一个列表列表。

So I tried doing something like this. 所以我尝试做这样的事情。 Might this solve your problem? 这样可以解决您的问题吗?

for key in dict:
    for val in key:
        if val == 7:
            print 'yes'

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

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