Python :: flatten一个不规则的列表os列表

时间:2017-07-21 22:04:09

标签: python list

我有一个不规则的数据结构,我想扁平化。这就是:

total_features = [[[{u'key': 11, u'tempo': 111.017, u'energy': 0.266, u'liveness': 0.197, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/02riVr97rs2edH8yBHprPQ', u'speechiness': 0.0412, u'uri': u'spotify:track:02riVr97rs2edH8yBHprPQ', u'acousticness': 0.845, u'danceability': 0.601, u'track_href': u'https://api.spotify.com/v1/tracks/02riVr97rs2edH8yBHprPQ', u'time_signature': 4, u'duration_ms': 294640, u'loudness': -16.373, u'mode': 0, u'valence': 0.46, u'type': u'audio_features', u'id': u'02riVr97rs2edH8yBHprPQ', u'instrumentalness': 0.268}], 
[{u'key': 11, u'tempo': 134.044, u'energy': 0.674, u'liveness': 0.126, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/12Vc7nkigVApJdXDSjLU06', u'speechiness': 0.0457, u'uri': u'spotify:track:12Vc7nkigVApJdXDSjLU06', u'acousticness': 0.0355, u'danceability': 0.869, u'track_href': u'https://api.spotify.com/v1/tracks/12Vc7nkigVApJdXDSjLU06', u'time_signature': 4, u'duration_ms': 206333, u'loudness': -6.238, u'mode': 0, u'valence': 0.915, u'type': u'audio_features', u'id': u'12Vc7nkigVApJdXDSjLU06', u'instrumentalness': 0.00151}], 
[{u'key': 9, u'tempo': 199.768, u'energy': 0.935, u'liveness': 0.359, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/5fKhUCZJL2HPVjnXp1U1oR', u'speechiness': 0.192, u'uri': u'spotify:track:5fKhUCZJL2HPVjnXp1U1oR', u'acousticness': 0.0729, u'danceability': 0.441, u'track_href': u'https://api.spotify.com/v1/tracks/5fKhUCZJL2HPVjnXp1U1oR', u'time_signature': 4, u'duration_ms': 45813, u'loudness': -2.79, u'mode': 1, u'valence': 0.947, u'type': u'audio_features', u'id': u'5fKhUCZJL2HPVjnXp1U1oR', u'instrumentalness': 0}], 
[{u'key': 4, u'tempo': 140.134, u'energy': 0.73, u'liveness': 0.136, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/4xZkjV9ZVbIfRzGpc5K8g8', u'speechiness': 0.0381, u'uri': u'spotify:track:4xZkjV9ZVbIfRzGpc5K8g8', u'acousticness': 0.0326, u'danceability': 0.483, u'track_href': u'https://api.spotify.com/v1/tracks/4xZkjV9ZVbIfRzGpc5K8g8', u'time_signature': 4, u'duration_ms': 315397, u'loudness': -5.515, u'mode': 0, u'valence': 0.207, u'type': u'audio_features', u'id': u'4xZkjV9ZVbIfRzGpc5K8g8', u'instrumentalness': 1.14e-05}]], 
[{u'key': 11, u'tempo': 180.985, u'energy': 0.549, u'liveness': 0.127, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/119c93MHjrDLJTApCVGpvx', u'speechiness': 0.373, u'uri': u'spotify:track:119c93MHjrDLJTApCVGpvx', u'acousticness': 0.57, u'danceability': 0.479, u'track_href': u'https://api.spotify.com/v1/tracks/119c93MHjrDLJTApCVGpvx', u'time_signature': 4, u'duration_ms': 252187, u'loudness': -10.551, u'mode': 0, u'valence': 0.573, u'type': u'audio_features', u'id': u'119c93MHjrDLJTApCVGpvx', u'instrumentalness': 0.0239}, 
{u'key': 2, u'tempo': 115.076, u'energy': 0.486, u'liveness': 0.12, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/6FRwDxXsvSasw0y2eDArsz', u'speechiness': 0.0385, u'uri': u'spotify:track:6FRwDxXsvSasw0y2eDArsz', u'acousticness': 0.436, u'danceability': 0.684, u'track_href': u'https://api.spotify.com/v1/tracks/6FRwDxXsvSasw0y2eDArsz', u'time_signature': 4, u'duration_ms': 250000, u'loudness': -12.521, u'mode': 1, u'valence': 0.821, u'type': u'audio_features', u'id': u'6FRwDxXsvSasw0y2eDArsz', u'instrumentalness': 9.77e-05}, 
{u'key': 2, u'tempo': 109.827, u'energy': 0.58, u'liveness': 0.0569, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/3Xls4cNOwy01dtrNXb1inG', u'speechiness': 0.0293, u'uri': u'spotify:track:3Xls4cNOwy01dtrNXb1inG', u'acousticness': 0.23, u'danceability': 0.547, u'track_href': u'https://api.spotify.com/v1/tracks/3Xls4cNOwy01dtrNXb1inG', u'time_signature': 4, u'duration_ms': 303200, u'loudness': -13.415, u'mode': 0, u'valence': 0.511, u'type': u'audio_features', u'id': u'3Xls4cNOwy01dtrNXb1inG', u'instrumentalness': 0.00877}, 
{u'key': 9, u'tempo': 132.112, u'energy': 0.773, u'liveness': 0.238, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/5AsLwfhLMlcFLvwdlTfd83', u'speechiness': 0.0411, u'uri': u'spotify:track:5AsLwfhLMlcFLvwdlTfd83', u'acousticness': 0.0609, u'danceability': 0.677, u'track_href': u'https://api.spotify.com/v1/tracks/5AsLwfhLMlcFLvwdlTfd83', u'time_signature': 4, u'duration_ms': 261973, u'loudness': -4.935, u'mode': 0, u'valence': 0.662, u'type': u'audio_features', u'id': u'5AsLwfhLMlcFLvwdlTfd83', u'instrumentalness': 0}, 
{u'key': 10, u'tempo': 144.07, u'energy': 0.67, u'liveness': 0.107, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/6ZA3Q8Dj5BrnmOsT300DCh', u'speechiness': 0.19, u'uri': u'spotify:track:6ZA3Q8Dj5BrnmOsT300DCh', u'acousticness': 0.167, u'danceability': 0.902, u'track_href': u'https://api.spotify.com/v1/tracks/6ZA3Q8Dj5BrnmOsT300DCh', u'time_signature': 4, u'duration_ms': 186672, u'loudness': -4.074, u'mode': 1, u'valence': 0.362, u'type': u'audio_features', u'id': u'6ZA3Q8Dj5BrnmOsT300DCh', u'instrumentalness': 0}, 
{u'key': 5, u'tempo': 127.913, u'energy': 0.696, u'liveness': 0.706, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/1KBE5FfTM6pgT3VGo1xo0Z', u'speechiness': 0.0358, u'uri': u'spotify:track:1KBE5FfTM6pgT3VGo1xo0Z', u'acousticness': 0.00483, u'danceability': 0.878, u'track_href': u'https://api.spotify.com/v1/tracks/1KBE5FfTM6pgT3VGo1xo0Z', u'time_signature': 4, u'duration_ms': 165068, u'loudness': -6.01, u'mode': 1, u'valence': 0.363, u'type': u'audio_features', u'id': u'1KBE5FfTM6pgT3VGo1xo0Z', u'instrumentalness': 0.309}, 
{u'key': 9, u'tempo': 172.023, u'energy': 0.935, u'liveness': 0.0977, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/2oLLOxkJWJj9BbrsTc4Xql', u'speechiness': 0.189, u'uri': u'spotify:track:2oLLOxkJWJj9BbrsTc4Xql', u'acousticness': 0.0318, u'danceability': 0.58, u'track_href': u'https://api.spotify.com/v1/tracks/2oLLOxkJWJj9BbrsTc4Xql', u'time_signature': 4, u'duration_ms': 191440, u'loudness': -3.348, u'mode': 1, u'valence': 0.574, u'type': u'audio_features', u'id': u'2oLLOxkJWJj9BbrsTc4Xql', u'instrumentalness': 0}, 
{u'key': 2, u'tempo': 110.512, u'energy': 0.642, u'liveness': 0.169, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/5duAEDKZd9Biayy9JBI6m2', u'speechiness': 0.0452, u'uri': u'spotify:track:5duAEDKZd9Biayy9JBI6m2', u'acousticness': 0.281, u'danceability': 0.545, u'track_href': u'https://api.spotify.com/v1/tracks/5duAEDKZd9Biayy9JBI6m2', u'time_signature': 5, u'duration_ms': 147800, u'loudness': -8.493, u'mode': 1, u'valence': 0.796, u'type': u'audio_features', u'id': u'5duAEDKZd9Biayy9JBI6m2', u'instrumentalness': 0.00652}, 
{u'key': 1, u'tempo': 90.447, u'energy': 0.902, u'liveness': 0.226, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/6SI4JD7iyQ0rrudhCNtMv0', u'speechiness': 0.291, u'uri': u'spotify:track:6SI4JD7iyQ0rrudhCNtMv0', u'acousticness': 0.219, u'danceability': 0.683, u'track_href': u'https://api.spotify.com/v1/tracks/6SI4JD7iyQ0rrudhCNtMv0', u'time_signature': 4, u'duration_ms': 253227, u'loudness': -7.389, u'mode': 0, u'valence': 0.708, u'type': u'audio_features', u'id': u'6SI4JD7iyQ0rrudhCNtMv0', u'instrumentalness': 0.000761}, 
{u'key': 7, u'tempo': 100.264, u'energy': 0.692, u'liveness': 0.528, u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/34v7Zs9a64h1xC3PWrmypP', u'speechiness': 0.0532, u'uri': u'spotify:track:34v7Zs9a64h1xC3PWrmypP', u'acousticness': 0.0275, u'danceability': 0.79, u'track_href': u'https://api.spotify.com/v1/tracks/34v7Zs9a64h1xC3PWrmypP', u'time_signature': 4, u'duration_ms': 235362, u'loudness': -4.704, u'mode': 0, u'valence': 0.816, u'type': u'audio_features', u'id': u'34v7Zs9a64h1xC3PWrmypP', u'instrumentalness': 0.369}]]

某些功能组在列表内部,其他功能则不在。这是一个常规列表操作列表,我可以这样做:

flattened = [val for sublist in total_features for val in sublist]

然后执行:

print [x['tempo'] for x in flattened]

但这在这里不起作用。

问题:

如何展平此结构,以便我可以打印所有“速度”值,例如?

1 个答案:

答案 0 :(得分:3)

您可以使用深度优先搜索来允许任意深度的嵌套列表:

def tempos(xs):
    while xs:
        x = xs.pop()
        if type(x) is dict:
            yield x['tempo']
        else:
            xs.extend(x)

print('\n'.join(tempos(total_features)))