Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按唯一值对列表进行分组_Python_List_Python 3.x_Sorting - Fatal编程技术网

Python 按唯一值对列表进行分组

Python 按唯一值对列表进行分组,python,list,python-3.x,sorting,Python,List,Python 3.x,Sorting,我有一张单子 list = [{'album': 'Spring Times', 'artist': 'Momo Pulse'}, {'album': 'Spring Times', 'artist': 'K.oshkin'}, {'album': 'Damn ', 'artist': 'Florent B'}] 我想将其分组以获得: list = [{'album': 'Spring Times', 'artist1': 'Momo Pulse', 'artist2': 'K.oshkin'

我有一张单子

list = [{'album': 'Spring Times', 'artist': 'Momo Pulse'}, {'album': 'Spring Times', 'artist': 'K.oshkin'}, {'album': 'Damn ', 'artist': 'Florent B'}]
我想将其分组以获得:

list = [{'album': 'Spring Times', 'artist1': 'Momo Pulse', 'artist2': 'K.oshkin'}, {'album': 'Damn ', 'artist1': 'Florent B'}]
我该怎么做? 有什么想法吗

from collections import defaultdict

l = [{'album': 'Spring Times', 'artist': 'Momo Pulse'}, 
     {'album': 'Spring Times', 'artist': 'K.oshkin'}, 
     {'album': 'Damn ', 'artist': 'Florent B'}]
d = defaultdict(list)

for record in l:
    d[record['album']].append(record['artist'])
我们现在有了一个字典,将专辑名称映射到艺术家列表

final = []

for album, artists in d.items():
    temp = {'album': album}
    for i, x in enumerate(artists, start=1):
        temp['artist{}'.format(i)] = x
    final.append(temp)

print(final)
印刷品

[{'album': 'Damn ', 'artist1': 'Florent B'}, {'album': 'Spring Times', 'artist1': 'Momo Pulse', 'artist2': 'K.oshkin'}]

字典是否只包含
'album'
'artist'
?这不是排序。@WillemVanOnsem是的,只有这个键确实是分组。@Aaron有史以来最好的道歉。
l = [{'album': 'Spring Times', 'artist': 'Momo Pulse'}, {'album': 'Spring Times', 'artist': 'K.oshkin'}, {'album': 'Damn ', 'artist': 'Florent B'}]

albums = {}
for el in l:
    album = albums.setdefault(el['album'], {})
    artist_id = sum(1 for k in album if k.startswith('artist'))
    album['artist'+str(artist_id)] = el['artist']

l2 = albums.values() #This is your output
from itertools import groupby

# input
my_list = [{'album': 'Spring Times', 'artist': 'Momo Pulse'}, {'album': 'Spring Times', 'artist': 'K.oshkin'}, {'album': 'Damn ', 'artist': 'Florent B'}]

# Have a function to return the merged dictionary after an update
def merge_dict(a, b):
    a.update(b)
    return a

# key function for sort and groupby
sortkey = lambda d: d['album']

# Sort and group by album
my_groups = groupby(sorted(my_list, key=sortkey), key=sortkey)

# Generate output
print [merge_dict({'album':k},{'artist'+str(i+1):d['artist'] for i, d in enumerate(g)}) for k, g in my_groups]