[英]Group dictionary by multiple keys in python
{
04-21: {
a: [1,2,3,4]
a: [5,6,7]
b: [8,9]
}
04-22: {
a: [1,3,4]
a: [2,6,7]
a: [3,8]
b: [1,4,2]
b: [8,9]
}
}
How do I group this dictionary by first and second value to produce:如何按第一个和第二个值对该字典进行分组以生成:
{
04-21:{
a: [1,2,3,4,5,6,7]
b: [8,9]
}
04-22:{
a: [1,3,4,2,6,7,3,8]
b: [1,4,2,8,9]
}
Following code gives me error:以下代码给了我错误:
for key, group in itertools.groupby(L, lambda x: x[0] ,x[1]):
print(key + " :", list(group))
As soon as you execute that dictionary in Python ie:只要您在 Python 中执行该字典,即:
d = {'04-21': {
'a': [1,2,3,4],
'a': [5,6,7],
'b': [8,9]
},
'04-22': {
'a': [1,3,4],
'a': [2,6,7],
'a': [3,8],
'b': [1,4,2],
'b': [8,9]
}
}
The dictionary becomes:字典变成:
{'04-21': {'a': [5, 6, 7], 'b': [8, 9]}, '04-22': {'a': [3, 8], 'b': [8, 9]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.