我有很多这样的字典:
data_sample = {'query_result': {'imdbid': 50083, 'file name': '12.Angry.Men.1957.Criterion.Collection.720p.BluRay.x264-WiKi.fre.srt', 'IDSubtitleFile': '1952985556'},
'movie directory': 'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'}
基本上:
data_collection = [data_sample] * 10
我想要的输出是一个元组,该元组包含:
(query_result["IDSubtitleFile"], movie_directory)
我将如何使用列表理解来创建所需的输出?我知道如何使用for循环来实现它,但是我正在努力使用列表理解来管理它。我唯一的想法是,但这会引发错误,并且实际上没有任何意义:
[[(value["IDSubtitleFile"], value) for value in data_sample.values()] for data_sample in data_collection]
写完这些之后,我想对于这种情况完全不使用列表推导可能更明智。
答案 0 :(得分:2)
这种理解会做到:
results = [
(d["query_result"]["IDSubtitleFile"], d["movie directory"])
for d in data_collection
]
结果:
>>> pprint([(d["query_result"]["IDSubtitleFile"], d["movie directory"]) for d in data_collection])
[('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem'),
('1952985556',
'C:\\...\\Movies\\12 Angry Men 1957 1080p BluRay x264 AAC - Ozlem')]