例如,我有四本字典的列表,比如
[{'username': 'xyz', 'label':'chemistry', 'marks': 56},
{'username': 'abc', 'label':'chemistry', 'marks': 95},
{'username': 'xyz', 'label':'math', 'marks': 43},
{'username': 'abc', 'label':'math', 'marks': 87}]
我想转换数据,这样我就可以得到数据
[{'username': 'xyz', 'chemistry': 56, 'math': 43},
{'username': 'abc', 'chemistry': 95, 'math': 87}]
下面是一个一次性解决方案,使用dict映射跟踪每个用户名的列表条目(假设dict列表存储在变量
l
):m
将变成:使用^{} :
这有点冗长,但能把工作做完。你知道吗
请注意,我在这里使用字典,因为字典上的查找是O(1),而不是列表上的O(n)。你知道吗
相关问题 更多 >
编程相关推荐