我有一个列表列表,它们是元组,看起来像:
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)]]
现在我实际上想将列表分类为字典。所以我使用了集合中的defaultdict:
d = defaultdict(list)
for k,v in z[0]:
d[k].append(v)
sorted(d.items())
我希望它将所有列表排序为字典。我尝试在z上循环,但它不起作用。我用过:
for i in np.arange(len(z)):
d = defaultdict(list)
for k,v in z[i]:
d[k].append(v)
sorted(d.items())
如何将字典列表作为大列表?
答案 0 :(得分:3)
您可以使用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]
结果:
[{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]}]
答案 1 :(得分:2)
我在解释您的问题,表示您希望对每个键的列表进行排序。在这种情况下:
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()}
所以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]}
根据您在下面的评论,可以将其简单地扩展到列表列表:
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)
给予
[[{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]}]]
答案 2 :(得分:0)
您也可以不使用任何导入来进行数据操作,就像这样
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)
,输出将如下所示:
[{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]}]