擅长:python、mysql、java
<pre><code>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'])
</code></pre>
<p>我们现在有了一个字典,将专辑名称映射到艺术家列表。你知道吗</p>
<pre><code>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)
</code></pre>
<p>印刷品</p>
<pre><code>[{'album': 'Damn ', 'artist1': 'Florent B'}, {'album': 'Spring Times', 'artist1': 'Momo Pulse', 'artist2': 'K.oshkin'}]
</code></pre>