简体   繁体   English

将3元素元组的列表转换为字典

[英]Converting List of 3 Element Tuple to Dictionary

If I have two List of tuples 如果我有两个元组列表

tuple2list=[(4, 21), (5, 10), (3, 8), (6, 7)]

tuple3list=[(4, 180, 21), (5, 90, 10), (3, 270, 8), (6, 0, 7)]

How do I convert it to a dictionary as below, 如何将其转换为字典,如下所示,

tuple2list2dict={4:21, 5:10, 3:8, 6:7}

tuple3list2dict={4: {180:21}, 5:{90:10}, 3:{270:8}, 6:{0:7}}

I know how to do it for 2 elements in tuples, using, 我知道如何为元组中的2个元素执行它,使用,

tuple2list2dict=dict((x[0], index) for index,x in enumerate(tuple2list))

But for 3 elements I have problem, have error trying the below, 但对于3个元素我有问题,尝试下面的错误,

tuple3list2dict=dict((x[0], dict(x[1], index)) for index,x in enumerate(tuple3list))

How do I reuse the above code for 3 element tuple to create a dictionary? 如何重用上面的3元组元组代码来创建字典?

Any pointer appreciated or point me where I could read more on this. 任何指针都赞赏或指出我可以阅读更多内容。 Have trouble finding it in the internet. 无法在互联网上找到它。

In Python2.7 or newer, you could use a dict comprehension: 在Python2.7或更高版本中,您可以使用dict理解:

In [100]: tuplelist = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)]

In [101]: tuplelist2dict = {a:{b:c} for a,b,c in tuplelist}

In [102]: tuplelist2dict
Out[102]: {3: {270: 8}, 4: {0: 7}, 5: {90: 10}}

In Python2.6 or older, the equivalent would be 在Python2.6或更早版本中,等价物将是

In [26]: tuplelist2dict = dict((a,{b:c}) for a,b,c in tuplelist)

Note that if the first value in the tuples occurs more than once, (as in the example above) the resulting tuplelist2dict only contains one key-value pair -- corresponding to the last tuple with the shared key. 请注意,如果元组中的第一个值出现多次,(如上例所示),生成的tuplelist2dict只包含一个键值对 - 对应于具有共享键的最后一个元组。

This pair-case is simple, since it aligns with dict construction : 这种情况很简单,因为它与dict结构一致:

... the positional argument must be an iterator object. ...位置参数必须是迭代器对象。 Each item in the iterable must itself be an iterator with exactly two objects . iterable中的每个项本身必须是一个只有两个对象的迭代器。 The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. 每个项目的第一个对象成为新词典中的一个键,第二个对象成为相应的值。

>>> t = [(4, 21), (5, 10), (3, 8), (4, 7)]
>>> dict(t)
{3: 8, 4: 7, 5: 10}

The triple case could be solved in this way: 三重案例可以通过这种方式解决:

>>> t = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)]
>>> dict([ (k, [v, w]) for k, v, w in t ])
{3: [270, 8], 4: [0, 7], 5: [90, 10]}

Or a bit more general: 或者更一般:

>>> dict([ (k[0], k[1:]) for k in t ]) # hello car, hi cdr
{3: (270, 8), 4: (0, 7), 5: (90, 10)}

Note that your code: 请注意您的代码:

_3_tuplelist_to_dict = {4: {180:21}, 5:{90:10}, 3:{270:8}, 4:{0:7}}

is really just a confusing representation of this: 实际上只是一个令人困惑的代表:

{3: {270: 8}, 4: {0: 7}, 5: {90: 10}}

Try: 尝试:

>>> {4: {180:21}, 5:{90:10}, 3:{270:8}, 4:{0:7}} == \
    {3: {270: 8}, 4: {0: 7}, 5: {90: 10}}
True

With Python 3, you can use a dict comprehension: 使用Python 3,您可以使用dict理解:

>>> t = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)]
>>> {key: values for key, *values in t}
{3: [270, 8], 4: [0, 7], 5: [90, 10]}

If one wants a nested dictionary without overriding the dictionary, could use the defaultdict from the collections library. 如果想要嵌套字典而不覆盖字典,可以使用collections库中的defaultdict

>>> from collections import defaultdict
>>> # Edited the list a bit to show when overrides
>>> tuple3list=[(4, 180, 21), (4, 90, 10), (3, 270, 8), (6, 0, 7)]
>>> tuple3dict = defaultdict(dict)
>>> for x, y, z in tuple3list:
...     tuple3dict[x][y] = z
... 
>>> print(tuple3dict)
defaultdict(<class 'dict'>, {4: {180: 21, 90: 10}, 3: {270: 8}, 6: {0: 7}})
>>> tuple3dict[4][90]
10

Unfortunately, one line assignments are tricky or impossible, hence I assume the only valid solution would be this. 不幸的是,一行分配很棘手或不可能,因此我认为唯一有效的解决方案就是这样。

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

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