[英]How to convert a list of lists with tuples into a list of dictionaries?
I have a list of lists which are tuples that look like:我有一个列表列表,它们是看起来像这样的元组:
z = [[(2, 81), (2, 52), (2, 53), (2, 86), (3, 4), (3, 5), (3, 72), (3, 9), (3, 16), (3, 17), (3, 18), (3, 84), (3, 73), (3, 41), (3, 63), (3, 30), (3, 31), (4, 48), (4, 5), (4, 38), (4, 7), (4, 8), (4, 95), (4, 11), (4, 12), (4, 16), (4, 19), (4, 53), (4, 54), (4, 57), (4, 27), (4, 30), (4, 37), (5, 51), (5, 67), (5, 6), (5, 65), (5, 11), (5, 15), (5, 19), (5, 21), (5, 24), (5, 36), (5, 71), (5, 31), (6, 83), (6, 71), (6, 76), (6, 98), (6, 78), (6, 16), (6, 19), (6, 20), (6, 22), (6, 87), (6, 52), (6, 27), (6, 86), (7, 49), (7, 33), (7, 60), (7, 96), (7, 43), (7, 12), (7, 14), (7, 15), (7, 17), (7, 46), (7, 22), (7, 28), (8, 32), (8, 34), (8, 98), (8, 40), (8, 96), (8, 10), (8, 44), (8, 13), (8, 84), (8, 21), (8, 87), (8, 28), (8, 30), (9, 65), (9, 40), (9, 14), (9, 55), (9, 25)], [(2, 32), (2, 3), (2, 39), (2, 8), (2, 9), (2, 10), (2, 43), (2, 13)], [(1, 85), (1, 22), (1, 23), (1, 56), (1, 25), (1, 70), (1, 42)], [(1, 79), (1, 17), (1, 67)], [(1, 54), (1, 6), (1, 7), (1, 92), (1, 10)], [(1, 3), (1, 4)], [(0, 91), (0, 92)], [(0, 88), (0, 89)], [(0, 70), (0, 74), (0, 83)], [(0, 66), (0, 68), (0, 69)], [(0, 26), (0, 29), (0, 33), (0, 35), (0, 38), (0, 41), (0, 45), (0, 50), (0, 51), (0, 55), (0, 57), (0, 59), (0, 65)], [(0, 24)], [(0, 14), (0, 15), (0, 20)],[(0, 13)], [(0, 9), (0, 11)], [(0, 7), (0, 8)], [(0, 5), (0, 6)], [(0, 3), (0, 4)]]
Now I actually want to sort the lists into dictionaries.现在我实际上想将列表排序到字典中。 So I used defaultdict from collections:
所以我使用了来自 collections 的 defaultdict:
d = defaultdict(list)
for k,v in z[0]:
d[k].append(v)
sorted(d.items())
I want it to sort all the lists into dictionaries.我希望它将所有列表排序到字典中。 I tried looping over z but it doesn't work.
我尝试在 z 上循环,但它不起作用。 I used:
我用了:
for i in np.arange(len(z)):
d = defaultdict(list)
for k,v in z[i]:
d[k].append(v)
sorted(d.items())
How do I get lists of dictionaries as the big list?如何将字典列表作为大列表?
You can use itertools.groupby
and operator.itemgetter
to group the keys and values from each tuple.您可以使用
itertools.groupby
和operator.itemgetter
对每个元组的键和值进行分组。
from itertools import groupby
from operator import itemgetter
[{k: sorted(map(itemgetter(1), v)) for k, v in groupby(l, itemgetter(0))} for l in z]
Results:结果:
[{2: [52, 53, 81, 86], 3: [4, 5, 9, 16, 17, 18, 30, 31, 41, 63, 72, 73, 84], 4: [5, 7, 8, 11, 12, 16, 19, 27, 30, 37, 38, 48, 53, 54, 57, 95], 5: [6, 11, 15, 19, 21, 24, 31, 36, 51, 65, 67, 71], 6: [16, 19, 20, 22, 27, 52, 71, 76, 78, 83, 86, 87, 98], 7: [12, 14, 15, 17, 22, 28, 33, 43, 46, 49, 60, 96], 8: [10, 13, 21, 28, 30, 32, 34, 40, 44, 84, 87, 96, 98], 9: [14, 25, 40, 55, 65]}, {2: [3, 8, 9, 10, 13, 32, 39, 43]}, {1: [22, 23, 25, 42, 56, 70, 85]}, {1: [17, 67, 79]}, {1: [6, 7, 10, 54, 92]}, {1: [3, 4]}, {0: [91, 92]}, {0: [88, 89]}, {0: [70, 74, 83]}, {0: [66, 68, 69]}, {0: [26, 29, 33, 35, 38, 41, 45, 50, 51, 55, 57, 59, 65]}, {0: [24]}, {0: [14, 15, 20]}, {0: [13]}, {0: [9, 11]}, {0: [7, 8]}, {0: [5, 6]}, {0: [3, 4]}]
I'm interpreting your question to mean that you want the lists to be sorted for each key.我将您的问题解释为您希望对每个键的列表进行排序。 In which case:
在这种情况下:
from collections import defaultdict
d = defaultdict(list)
for k,v in z[0]:
d[k].append(v)
d = {k: sorted(v) for k, v in d.items()}
So d becomes:所以 d 变为:
{2: [52, 53, 81, 86],
3: [4, 5, 9, 16, 17, 18, 30, 31, 41, 63, 72, 73, 84],
4: [5, 7, 8, 11, 12, 16, 19, 27, 30, 37, 38, 48, 53, 54, 57, 95],
5: [6, 11, 15, 19, 21, 24, 31, 36, 51, 65, 67, 71],
6: [16, 19, 20, 22, 27, 52, 71, 76, 78, 83, 86, 87, 98],
7: [12, 14, 15, 17, 22, 28, 33, 43, 46, 49, 60, 96],
8: [10, 13, 21, 28, 30, 32, 34, 40, 44, 84, 87, 96, 98],
9: [14, 25, 40, 55, 65]}
Per your comment below, this can simply be extended to the list of lists:根据您在下面的评论,这可以简单地扩展到列表列表:
out = []
for entry in z:
d = defaultdict(list)
for k,v in entry:
d[k].append(v)
out.append([{k: sorted(v) for k, v in d.items()}])
print(out)
Gives给
[[{2: [52, 53, 81, 86],
3: [4, 5, 9, 16, 17, 18, 30, 31, 41, 63, 72, 73, 84],
4: [5, 7, 8, 11, 12, 16, 19, 27, 30, 37, 38, 48, 53, 54, 57, 95],
5: [6, 11, 15, 19, 21, 24, 31, 36, 51, 65, 67, 71],
6: [16, 19, 20, 22, 27, 52, 71, 76, 78, 83, 86, 87, 98],
7: [12, 14, 15, 17, 22, 28, 33, 43, 46, 49, 60, 96],
8: [10, 13, 21, 28, 30, 32, 34, 40, 44, 84, 87, 96, 98],
9: [14, 25, 40, 55, 65]}],
[{2: [3, 8, 9, 10, 13, 32, 39, 43]}],
[{1: [22, 23, 25, 42, 56, 70, 85]}],
[{1: [17, 67, 79]}],
[{1: [6, 7, 10, 54, 92]}],
[{1: [3, 4]}],
[{0: [91, 92]}],
[{0: [88, 89]}],
[{0: [70, 74, 83]}],
[{0: [66, 68, 69]}],
[{0: [26, 29, 33, 35, 38, 41, 45, 50, 51, 55, 57, 59, 65]}],
[{0: [24]}],
[{0: [14, 15, 20]}],
[{0: [13]}],
[{0: [9, 11]}],
[{0: [7, 8]}],
[{0: [5, 6]}],
[{0: [3, 4]}]]
You can also do it without using any imports for data manipulation like so您也可以在不使用任何导入进行数据操作的情况下这样做
from pprint import pprint
l = []
for list_item in z:
d = {}
for index, value in list_item:
if index not in d:
d[index] = [value]
else:
d[index].append(value)
l.append({k: sorted(v) for k, v in d.items()})
pprint(l)
and the output will be like so: output 将如下所示:
[{2: [52, 53, 81, 86],
3: [4, 5, 9, 16, 17, 18, 30, 31, 41, 63, 72, 73, 84],
4: [5, 7, 8, 11, 12, 16, 19, 27, 30, 37, 38, 48, 53, 54, 57, 95],
5: [6, 11, 15, 19, 21, 24, 31, 36, 51, 65, 67, 71],
6: [16, 19, 20, 22, 27, 52, 71, 76, 78, 83, 86, 87, 98],
7: [12, 14, 15, 17, 22, 28, 33, 43, 46, 49, 60, 96],
8: [10, 13, 21, 28, 30, 32, 34, 40, 44, 84, 87, 96, 98],
9: [14, 25, 40, 55, 65]},
{2: [3, 8, 9, 10, 13, 32, 39, 43]},
{1: [22, 23, 25, 42, 56, 70, 85]},
{1: [17, 67, 79]},
{1: [6, 7, 10, 54, 92]},
{1: [3, 4]},
{0: [91, 92]},
{0: [88, 89]},
{0: [70, 74, 83]},
{0: [66, 68, 69]},
{0: [26, 29, 33, 35, 38, 41, 45, 50, 51, 55, 57, 59, 65]},
{0: [24]},
{0: [14, 15, 20]},
{0: [13]},
{0: [9, 11]},
{0: [7, 8]},
{0: [5, 6]},
{0: [3, 4]}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.