我有一个清单:
list = [
{'album': '1', 'artist': 'pedro', 'title': 'Duhast'},
{'album': '2', 'artist': 'hose', 'title':'Star boy'},
{'album': '1', 'artist': 'migel', 'title': 'Lemon tree'}
]
我需要这样对列表进行分组/排序:
list = [
{'album': '1',
'tracks': [
{'artist': 'pedro', 'title': 'Duhast'},
{'artist': 'migel', 'title': 'Lemon tree'}]
},
{'album': '2',
'tracks':[
{'artist': 'hose', 'title':'Star boy'}]
}
]
更确切地说,我需要按专辑对曲目进行分组。有什么办法让这件事简单些吗?你知道吗
1-衬里:)
https://repl.it/HA48/2
正如@Prune提到的,
groupby
函数可用于按指定的键函数对列表进行分组。为了使其工作,列表必须按键排序。你知道吗就我个人而言,我觉得上面的解决方案有点难以理解。。。这会产生相同的结果:
https://repl.it/HA48/1
相关问题 更多 >
编程相关推荐