简体   繁体   English

Python:迭代嵌套字典

[英]Python: iterate over a nested dictionary

given the following dictionary : 给出以下dictionary

playlists={'user':[
               {'playlist1':{
                    'tracks': [        
                    {'name': 'Karma Police','artist': 'Radiohead', 'count': "1.0"},
                    {'name': 'Bitter Sweet Symphony','artist': 'The Verve','count': "2.0"}                    
                     ]
                    }
               },
               {'playlist2':{
                    'tracks': [        
                    {'name': 'We Will Rock You','artist': 'Queen', 'count': "3.0"},
                    {'name': 'Roxanne','artist': 'Police','count': "5.0"}                    
                     ]
                  }
                },
              ]
            }

though not very elegantly, I can fetch keys and values in such a structure: 虽然不是很优雅,但我可以在这样的结构中获取键和值:

keys : keys

userX = [user[0] for user in playlists.iteritems()][0] //userX
playlist1 = list(playlists['userX'][0].keys())[0] //playlist1

and nested values : 和嵌套values

artist = playlists['user'][0]["playlist1"]['tracks'][0]['artist'] //Radiohead
count = playlists['user'][1]["playlist2"]['tracks'][0]['count'] //3.0

but I'm a bit confused as to how access all playlists names for playlists and all artists for tracks in playlists using any iteration tool. 但是我对使用任何iteration工具如何访问playlists names for playlists和所有artists for tracks in playlists所有playlists names for playlists感到困惑。

how can I do it? 我该怎么做?

You can use following code to access the playlist names and artist names: 您可以使用以下代码访问播放列表名称和艺术家姓名:

# Define generator to return dicts under each user
lists = (x for user in playlists.itervalues() for x in user)
print [name for play_list in lists for name in play_list.iterkeys()]

lists = (x for user in playlists.itervalues() for x in user)
track_lists = (content['tracks'] for p in lists for content in p.itervalues())
print [track['artist'] for track_list in track_lists for track in track_list]

Output: 输出:

['playlist1', 'playlist2']
['Radiohead', 'The Verve', 'Queen', 'Police']

Note that above works only with Python 2, with Python 3 values & keys should be used instead of itervalues & iterkeys . 请注意,上面只适用于Python 2,应使用Python 3 valueskeys而不是itervaluesiterkeys

If you are looking to extract a flat list of all playlist names you can use a list comprehension. 如果您要提取所有播放列表名称的平面列表,可以使用列表推导。 The mix of dictionaries and lists adds a little complexity. 字典和列表的混合增加了一点复杂性。

all_playlists = [ playlist_title 
                  for user in playlists 
                  for user_playlists in playlists[user]
                  for playlist_title in user_playlists ]
print all_playlists

all_artists = [ track['artist']
                for user in playlists
                for user_playlists in playlists[user]
                for playlist_title in user_playlists 
                for track in user_playlists[playlist_title]['tracks'] ]
print all_artists

gives

['playlist1', 'playlist2']
['Radiohead', 'The Verve', 'Queen', 'Police']

Note that this will give combined lists of playlists/artists across all users in the playlists dict 请注意,这将为播放列表中的所有用户提供播放列表/艺术家的组合列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM