If a create a dictionary in this fashion:
output = {'playlist': {}}
having a list
like this:
tracks = [u'No Surprises', u'Oceans', u'Up in Flames']
and with
count = 0
I would like to populate the key playlist
with all tracks
items as keys
with 0
value, like this:
output = {'playlist': {
'No Surprises': 0,
'Oceans': 0,
'Up in Flames': 0}}
How do I do that?
>>> output['playlist'] = dict(zip(tracks, [value for i in tracks]))
>>> output
{'playlist': {u'Up in Flames': 0, u'Oceans': 0, u'No Surprises': 0}}
output = {'playlist':{track: count for track in tracks}}
迟了将近两年,但这似乎是dict.fromkeys
的理想用例:
output = {'playlist': dict.fromkeys(tracks, count)}
for track in tracks:
output["playlist"][track] = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.