如何将元组转换为字典?
How to convert a tuple to a dict?
我有一个元组列表(每个元组有 3 个元素),我想将它转换成字典,如何以最有效的方式做到这一点?这是示例:
[(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)]
我想要下面的字典:
{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}}
此处元组的第一个元素是键,下一个元素成为第一个元素字典中的键,最后一个元素成为第二个键的值。
我尝试了以下方法,但它给出的字典不完整:
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}}
你的
{a:{b:c} for a,b,c in e}
覆盖主字典的值。如果主词典已经存在,您可以使用 setdefault
访问主词典的值:
d = {}
for a, b, c in e:
d.setdefault(a, {})[b] = c
您的代码只保留给定键的第一个遇到的值。您可以使用 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)
输出:
{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}}
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}
我有一个元组列表(每个元组有 3 个元素),我想将它转换成字典,如何以最有效的方式做到这一点?这是示例:
[(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)]
我想要下面的字典:
{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}}
此处元组的第一个元素是键,下一个元素成为第一个元素字典中的键,最后一个元素成为第二个键的值。
我尝试了以下方法,但它给出的字典不完整:
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}}
你的
{a:{b:c} for a,b,c in e}
覆盖主字典的值。如果主词典已经存在,您可以使用 setdefault
访问主词典的值:
d = {}
for a, b, c in e:
d.setdefault(a, {})[b] = c
您的代码只保留给定键的第一个遇到的值。您可以使用 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)
输出:
{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}}
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}