列出dict中tuple键的所有值

时间:2021-11-23 18:09:36

Suppose I have a dictionary with a tuple as key, such as the following:

假设我有一个以元组为键的字典,如下所示:

d1 = {}
d1[(111,1)] = "value1111"
d1[(111,2)] = "value1112"
d1[(111,3)] = "value1113"
d1[(112,1)] = "value1121"
d1[(112,2)] = "value1122"
d1[(112,3)] = "value1123"

How can I get all the values for a given number in the first element of the tuple key? That is, for the 111, I want to obtain the following:

如何在元组键的第一个元素中获取给定数字的所有值?也就是说,对于111,我想获得以下内容:

value1111
value1112
value1113

I've tried print(d1[(111,i)]) but it only returns one value, is there a simple way of doing this?

我试着打印(D1 [(111 I)]),但它只返回一个值,是有这样做的一个简单的方法?

Thanks in advance.

提前致谢。

4 个解决方案

#1


8  

Use list comprehension, this way:

使用list comprehension,这样:

[v for k,v in d1.items() if k[0]==111]

The condition if k[0]==111 means return only values of d1 whose key's first element is 111

如果k [0] == 111则表示仅返回其键的第一个元素为111的d1的值

You were also trying with print(d1[(111,i)],that will work if you have a control over the range and types of the second element of key's tuple, i.e:

您还尝试使用print(d1 [(111,i)],如果您可以控制键元组的第二个元素的范围和类型,那么它将起作用,即:

>>> [d1[(111,i)] for i in range(1,4)]
['value1111', 'value1112', 'value1113']

In your example, we know that the i can only be from 1 to 3, hence range(1,4), but if you don't know what's the range of i and even what the type of i could be, then the list comprehension is your best friend here.

在你的例子中,我们知道i只能从1到3,因此范围(1,4),但是如果你不知道i的范围是什么,甚至我的类型是什么,那么列表理解是你最好的朋友。

Of course, one can get the list of second element of key's tuple whose first element is 111 by doing so:

当然,通过这样做,可以得到第一个元素为111的键元组的第二个元素列表:

>>>my_filter = [k[1] for k in d1 if k[0]==111]
[2, 3, 1]
>>>my_list = [d1[(111,i) for i in my_filter]
['value1112', 'value1113', 'value1111']

#2


7  

A simple list comprehension will do the trick:

一个简单的列表理解就可以了:

>>> [d1[tup] for tup in d1 if tup[0] == 111]
['value1112', 'value1113', 'value1111']

It says "for every key tup in the dictionary, give me the corresponding value if the key's first element is 111".

它说“对于字典中的每个关键tup,如果键的第一个元素是111,给我相应的值”。

#3


2  

If you control how the dict is created and want to group by the first element of your tuples forget using tuples as keys and use a defaultdict appending to a list to handle repeated keys:

如果您控制dict的创建方式并希望按元组的第一个元素进行分组,请忘记使用元组作为键,并使用附加到列表的defaultdict来处理重复的键:

from collections import defaultdict
d1 = defaultdict(list)
d1[111].append("value1111")
d1[111].append( "value1112")
d1[111].append("value1113")
d1[112].append("value1121")
d1[112].append("value1122")
d1[112].apendd("value1123")

Then you can get all the values at once or index the list to pull the ith value:

然后,您可以一次获取所有值或索引列表以获取第i个值:

In [17]: d1

Out[17]: 
defaultdict(list,
            {111: ['value1111', 'value1112', 'value1113'],
             112: ['value1121', 'value1122']})
In [18]: d1[111]
Out[18]: ['value1111', 'value1112', 'value1113']

In [19]: d1[111][0]
Out[19]: 'value1111'

In [20]: d1[111][1]
Out[20]: 'value1112'

#4


0  

Honestly, I think the default dict approach is the most flexible and allows you to query other values easily (e.g. 112 or 113). In the code below, d2 will be a map from the 111, 112, 113 to their respective values.

老实说,我认为默认的dict方法是最灵活的,允许您轻松查询其他值(例如112或113)。在下面的代码中,d2将是从111,112,113到它们各自值的映射。

d2 = defaultdict(list)
for key, value in d1.iteritems():
    d2[key[0]].append(value)

print d2[111]
print d2[112]

#1


8  

Use list comprehension, this way:

使用list comprehension,这样:

[v for k,v in d1.items() if k[0]==111]

The condition if k[0]==111 means return only values of d1 whose key's first element is 111

如果k [0] == 111则表示仅返回其键的第一个元素为111的d1的值

You were also trying with print(d1[(111,i)],that will work if you have a control over the range and types of the second element of key's tuple, i.e:

您还尝试使用print(d1 [(111,i)],如果您可以控制键元组的第二个元素的范围和类型,那么它将起作用,即:

>>> [d1[(111,i)] for i in range(1,4)]
['value1111', 'value1112', 'value1113']

In your example, we know that the i can only be from 1 to 3, hence range(1,4), but if you don't know what's the range of i and even what the type of i could be, then the list comprehension is your best friend here.

在你的例子中,我们知道i只能从1到3,因此范围(1,4),但是如果你不知道i的范围是什么,甚至我的类型是什么,那么列表理解是你最好的朋友。

Of course, one can get the list of second element of key's tuple whose first element is 111 by doing so:

当然,通过这样做,可以得到第一个元素为111的键元组的第二个元素列表:

>>>my_filter = [k[1] for k in d1 if k[0]==111]
[2, 3, 1]
>>>my_list = [d1[(111,i) for i in my_filter]
['value1112', 'value1113', 'value1111']

#2


7  

A simple list comprehension will do the trick:

一个简单的列表理解就可以了:

>>> [d1[tup] for tup in d1 if tup[0] == 111]
['value1112', 'value1113', 'value1111']

It says "for every key tup in the dictionary, give me the corresponding value if the key's first element is 111".

它说“对于字典中的每个关键tup,如果键的第一个元素是111,给我相应的值”。

#3


2  

If you control how the dict is created and want to group by the first element of your tuples forget using tuples as keys and use a defaultdict appending to a list to handle repeated keys:

如果您控制dict的创建方式并希望按元组的第一个元素进行分组,请忘记使用元组作为键,并使用附加到列表的defaultdict来处理重复的键:

from collections import defaultdict
d1 = defaultdict(list)
d1[111].append("value1111")
d1[111].append( "value1112")
d1[111].append("value1113")
d1[112].append("value1121")
d1[112].append("value1122")
d1[112].apendd("value1123")

Then you can get all the values at once or index the list to pull the ith value:

然后,您可以一次获取所有值或索引列表以获取第i个值:

In [17]: d1

Out[17]: 
defaultdict(list,
            {111: ['value1111', 'value1112', 'value1113'],
             112: ['value1121', 'value1122']})
In [18]: d1[111]
Out[18]: ['value1111', 'value1112', 'value1113']

In [19]: d1[111][0]
Out[19]: 'value1111'

In [20]: d1[111][1]
Out[20]: 'value1112'

#4


0  

Honestly, I think the default dict approach is the most flexible and allows you to query other values easily (e.g. 112 or 113). In the code below, d2 will be a map from the 111, 112, 113 to their respective values.

老实说,我认为默认的dict方法是最灵活的,允许您轻松查询其他值(例如112或113)。在下面的代码中,d2将是从111,112,113到它们各自值的映射。

d2 = defaultdict(list)
for key, value in d1.iteritems():
    d2[key[0]].append(value)

print d2[111]
print d2[112]