简体   繁体   English

如何将元组转换为字典?

[英]How to convert a tuple to a dict?

I have a list of tuple(each tuple is of 3 elements) and I want to convert it into dictionary, how to do it in most efficient way? 我有一个元组列表(每个元组包含3个元素),我想将其转换为字典,如何以最有效的方式进行处理? Here is the example: 这是示例:

[(980898, 9977, 1),
 (899979879, 23, 1),
 (1, 1, 2),
 (980898, 98789797, 1),
 (98789797, 980898, 1),
 (1, 756735, 1),
 (1, 3344, 1),
 (23, 4, 1),
 (534, 23, 1),
 (756735, 1, 1),
 (9977, 980898, 1),
 (23, 899979879, 1),
 (4, 23, 1),
 (756735, 980898, 1),
 (3344, 1, 1),
 (980898, 756735, 1),
 (23, 534, 1)]

I want the following dict: 我想要以下命令:

{1: {1: 2, 3344: 1, 756735: 1},
 4: {23: 1},
 23: {4: 1, 534: 1, 899979879: 1},
 534: {23: 1},
 3344: {1: 1},
 9977: {980898: 1},
 756735: {1: 1, 980898: 1},
 980898: {9977: 1, 756735: 1, 98789797: 1},
 98789797: {980898: 1},
 899979879: {23: 1}}

Here first element of the tuple is the key and next element becomes the key in the dict of first element and final element becomes the value for the second key. 在此,元组的第一个元素是键,而下一个元素成为第一个元素的字典中的键,而最后一个元素则成为第二个键的值。

I have tried the following but it gives incomplete dictionary : 我尝试了以下方法,但给出的字典不完整:

finalDict = {a:{b:c} for a,b,c in e}
print finalDict

{1: {3344: 1}, 980898: {756735: 1}, 4: {23: 1}, 98789797: {980898: 1}, 899979879: {23: 1}, 3344: {1: 1}, 534: {23: 1}, 23: {534: 1}, 9977: {980898: 1}, 756735: {980898: 1}}
d = [(980898, 9977, 1),
 (899979879, 23, 1),
 (1, 1, 2),
 (980898, 98789797, 1),
 (98789797, 980898, 1),
 (1, 756735, 1),
 (1, 3344, 1),
 (23, 4, 1),
 (534, 23, 1),
 (756735, 1, 1),
 (9977, 980898, 1),
 (23, 899979879, 1),
 (4, 23, 1),
 (756735, 980898, 1),
 (3344, 1, 1),
 (980898, 756735, 1),
 (23, 534, 1)]

from collections import defaultdict

D = defaultdict(dict)

for a, b, c in d:
    D[a][b] = c

for k in sorted(D):
    print k, D[k]

1 {3344: 1, 1: 2, 756735: 1}
4 {23: 1}
23 {4: 1, 534: 1, 899979879: 1}
534 {23: 1}
3344 {1: 1}
9977 {980898: 1}
756735 {1: 1, 980898: 1}
980898 {9977: 1, 98789797: 1, 756735: 1}
98789797 {980898: 1}
899979879 {23: 1}

Your 你的

{a:{b:c} for a,b,c in e}

overwrites the values of the main dictionary. 覆盖主词典的值。 You can use setdefault to access the value of the main dictionary if it already exists: 您可以使用setdefault访问主字典的值(如果已存在):

d = {}
for a, b, c in e:
    d.setdefault(a, {})[b] = c

Your code only keeps the first encountered value for a given key. 您的代码仅保留给定键的第一个遇到的值。 You can use defaultdict from the collections module to initialize result with a fresh dict and then set the key to the value as encountered in your list of tuples. 您可以使用collections模块中的defaultdict用新的dict初始化result ,然后将键设置为元组列表中遇到的值。

#all_tuples = [(...), (...), ...]
from collections import defaultdict

result = defaultdict(dict)
for uid, k, v in all_tuples :
    result[uid][k] = v

dict(result)

Output: 输出:

{1: {1: 2, 3344: 1, 756735: 1},
 4: {23: 1},
 23: {4: 1, 534: 1, 899979879: 1},
 534: {23: 1},
 3344: {1: 1},
 9977: {980898: 1},
 756735: {1: 1, 980898: 1},
 980898: {9977: 1, 756735: 1, 98789797: 1},
 98789797: {980898: 1},
 899979879: {23: 1}}

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

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