我将对象zipped
实例化如下:
zipped = zip(_artist_name, _artist_title, _xl, _l, _m, _s)
其中参数均为30个字符串的数组。我有一个字典track_data
,在密钥recent_tracks
下面,我还有其他键值对,如下所示:
track_data['recent_tracks'] = [
{"artist": ?,
"title": ?,
"image_xl": ?,
"image_l": ?,
"image_m": ?,
"image_s": ?}
]
我希望track_data['recent_tracks']
包含30个对象,其中每个对象包含6个子对象。有点像这样:
object 1 - artist
title
xl
l
m
s
object 2 - artist
title
xl
l
m
s
我之前的方法与上面的示例类似,但它只创建了一个包含30个对象的6个数组的对象。如何遍历压缩项以将正确的值分配给30个单独对象中的相应对?
答案 0 :(得分:2)
添加密钥并为每个zipped
条目生成字典:
track_keys = ("artist", "title", "image_xl", "image_l", "image_m", "image_s")
zipped = zip(_artist_name, _artist_title, _xl, _l, _m, _s)
track_data['recent_tracks'] = [dict(zip(track_keys, values)) for values in zipped]
演示:
>>> _artist_name = ['artist {}'.format(i + 1) for i in range(30)]
>>> _artist_title = ['title {}'.format(i + 1) for i in range(30)]
>>> _xl = ['xl image {}'.format(i + 1) for i in range(30)]
>>> _l = ['l image {}'.format(i + 1) for i in range(30)]
>>> _m = ['m image {}'.format(i + 1) for i in range(30)]
>>> _s = ['s image {}'.format(i + 1) for i in range(30)]
>>>
>>> from pprint import pprint
>>> track_keys = ("artist", "title", "image_xl", "image_l", "image_m", "image_s")
>>> zipped = zip(_artist_name, _artist_title, _xl, _l, _m, _s)
>>> pprint([dict(zip(track_keys, values)) for values in zipped])
[{'artist': 'artist 1',
'image_l': 'l image 1',
'image_m': 'm image 1',
'image_s': 's image 1',
'image_xl': 'xl image 1',
'title': 'title 1'},
{'artist': 'artist 2',
'image_l': 'l image 2',
'image_m': 'm image 2',
'image_s': 's image 2',
'image_xl': 'xl image 2',
'title': 'title 2'},
{'artist': 'artist 3',
'image_l': 'l image 3',
'image_m': 'm image 3',
'image_s': 's image 3',
'image_xl': 'xl image 3',
'title': 'title 3'},
{'artist': 'artist 4',
'image_l': 'l image 4',
'image_m': 'm image 4',
'image_s': 's image 4',
'image_xl': 'xl image 4',
'title': 'title 4'},
{'artist': 'artist 5',
'image_l': 'l image 5',
'image_m': 'm image 5',
'image_s': 's image 5',
'image_xl': 'xl image 5',
'title': 'title 5'},
{'artist': 'artist 6',
'image_l': 'l image 6',
'image_m': 'm image 6',
'image_s': 's image 6',
'image_xl': 'xl image 6',
'title': 'title 6'},
{'artist': 'artist 7',
'image_l': 'l image 7',
'image_m': 'm image 7',
'image_s': 's image 7',
'image_xl': 'xl image 7',
'title': 'title 7'},
{'artist': 'artist 8',
'image_l': 'l image 8',
'image_m': 'm image 8',
'image_s': 's image 8',
'image_xl': 'xl image 8',
'title': 'title 8'},
{'artist': 'artist 9',
'image_l': 'l image 9',
'image_m': 'm image 9',
'image_s': 's image 9',
'image_xl': 'xl image 9',
'title': 'title 9'},
{'artist': 'artist 10',
'image_l': 'l image 10',
'image_m': 'm image 10',
'image_s': 's image 10',
'image_xl': 'xl image 10',
'title': 'title 10'},
{'artist': 'artist 11',
'image_l': 'l image 11',
'image_m': 'm image 11',
'image_s': 's image 11',
'image_xl': 'xl image 11',
'title': 'title 11'},
{'artist': 'artist 12',
'image_l': 'l image 12',
'image_m': 'm image 12',
'image_s': 's image 12',
'image_xl': 'xl image 12',
'title': 'title 12'},
{'artist': 'artist 13',
'image_l': 'l image 13',
'image_m': 'm image 13',
'image_s': 's image 13',
'image_xl': 'xl image 13',
'title': 'title 13'},
{'artist': 'artist 14',
'image_l': 'l image 14',
'image_m': 'm image 14',
'image_s': 's image 14',
'image_xl': 'xl image 14',
'title': 'title 14'},
{'artist': 'artist 15',
'image_l': 'l image 15',
'image_m': 'm image 15',
'image_s': 's image 15',
'image_xl': 'xl image 15',
'title': 'title 15'},
{'artist': 'artist 16',
'image_l': 'l image 16',
'image_m': 'm image 16',
'image_s': 's image 16',
'image_xl': 'xl image 16',
'title': 'title 16'},
{'artist': 'artist 17',
'image_l': 'l image 17',
'image_m': 'm image 17',
'image_s': 's image 17',
'image_xl': 'xl image 17',
'title': 'title 17'},
{'artist': 'artist 18',
'image_l': 'l image 18',
'image_m': 'm image 18',
'image_s': 's image 18',
'image_xl': 'xl image 18',
'title': 'title 18'},
{'artist': 'artist 19',
'image_l': 'l image 19',
'image_m': 'm image 19',
'image_s': 's image 19',
'image_xl': 'xl image 19',
'title': 'title 19'},
{'artist': 'artist 20',
'image_l': 'l image 20',
'image_m': 'm image 20',
'image_s': 's image 20',
'image_xl': 'xl image 20',
'title': 'title 20'},
{'artist': 'artist 21',
'image_l': 'l image 21',
'image_m': 'm image 21',
'image_s': 's image 21',
'image_xl': 'xl image 21',
'title': 'title 21'},
{'artist': 'artist 22',
'image_l': 'l image 22',
'image_m': 'm image 22',
'image_s': 's image 22',
'image_xl': 'xl image 22',
'title': 'title 22'},
{'artist': 'artist 23',
'image_l': 'l image 23',
'image_m': 'm image 23',
'image_s': 's image 23',
'image_xl': 'xl image 23',
'title': 'title 23'},
{'artist': 'artist 24',
'image_l': 'l image 24',
'image_m': 'm image 24',
'image_s': 's image 24',
'image_xl': 'xl image 24',
'title': 'title 24'},
{'artist': 'artist 25',
'image_l': 'l image 25',
'image_m': 'm image 25',
'image_s': 's image 25',
'image_xl': 'xl image 25',
'title': 'title 25'},
{'artist': 'artist 26',
'image_l': 'l image 26',
'image_m': 'm image 26',
'image_s': 's image 26',
'image_xl': 'xl image 26',
'title': 'title 26'},
{'artist': 'artist 27',
'image_l': 'l image 27',
'image_m': 'm image 27',
'image_s': 's image 27',
'image_xl': 'xl image 27',
'title': 'title 27'},
{'artist': 'artist 28',
'image_l': 'l image 28',
'image_m': 'm image 28',
'image_s': 's image 28',
'image_xl': 'xl image 28',
'title': 'title 28'},
{'artist': 'artist 29',
'image_l': 'l image 29',
'image_m': 'm image 29',
'image_s': 's image 29',
'image_xl': 'xl image 29',
'title': 'title 29'},
{'artist': 'artist 30',
'image_l': 'l image 30',
'image_m': 'm image 30',
'image_s': 's image 30',
'image_xl': 'xl image 30',
'title': 'title 30'}]