Python 3.x 我有一个字典列表,每个字典都有一个分配给键的值,我需要根据其他两个值获取这个值
这是列表Python 3.x 我有一个字典列表,每个字典都有一个分配给键的值,我需要根据其他两个值获取这个值,python-3.x,list,Python 3.x,List,这是列表{'id':142840,“posters':[{'aspect_ratio':0.703125,'file_path':'/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg','height':2048,'iso_639_1':'en','vote_average':5.312 vote_count:1,'width':1440},{'aspect_ratio':0.70703125,'file_count':1,'width':1448},{'aspect_ratio'
{'id':142840,“posters':[{'aspect_ratio':0.703125,'file_path':'/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg','height':2048,'iso_639_1':'en','vote_average':5.312 vote_count:1,'width':1440},{'aspect_ratio':0.70703125,'file_count':1,'width':1448},{'aspect_ratio':0.7068881289692233,'file_path':'6mlag60xeqmw0ga8tdfzsk0r1.jpg,'height':2047,'iso_639_'1':None,'aspect_count':1,'width':1448},{'aspect_ratio_ratio':0,'average':宽度}
我正在尝试选择
文件路径的值
如果高度
>宽度
由于高度和宽度仅在最后一个字典中,您可以按如下所示编写代码:
if d['posters'][2]['height'] > d['posters'][2]['width'] :
return d['posters'][2]['file_path']
如果“高度”和“宽度”不在上一个字典中,请更改“[2]”的值
希望对您有所帮助。;-由于高度和宽度仅在上一本字典中,您可以编写如下代码:
if d['posters'][2]['height'] > d['posters'][2]['width'] :
return d['posters'][2]['file_path']
如果“高度”和“宽度”不在上一个字典中,请更改“[2]”的值
希望对您有所帮助。-设置复杂数据结构的格式通常有助于更好地了解各部分之间的关系。在这种情况下,它表明您关心的信息(
高度
,宽度
,以及文件路径
)可以在dict中的海报
列表中的字典中找到这些信息,我们可以称之为海报
。利用这些信息,我们可以构建一个满足指定的高度
和宽度
关系的文件路径列表
例如:
poster_dict = {
"id": 142840,
"posters": [
{
"aspect_ratio": 0.703125,
"file_path": "/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg",
"height": 2048,
"iso_639_1": "en",
"vote_average": 5.312,
"vote_count": 1,
"width": 1440,
},
{
"aspect_ratio": 0.70703125,
"file_path": "/2Lgb7oPgqVpJmruRoH7Rs2yJH0n.jpg",
"height": 2048,
"iso_639_1": "en",
"vote_average": 5.172,
"vote_count": 1,
"width": 1448,
},
{
"aspect_ratio": 0.7068881289692233,
"file_path": "/6mLaG60xEQm6mw0Ga8TDFZsk0R1.jpg",
"height": 2047,
"iso_639_1": None,
"vote_average": 0.0,
"vote_count": 0,
"width": 1447,
},
],
}
file_path_list = [
one_poster["file_path"]
for one_poster in poster_dict["posters"]
if one_poster["height"] > one_poster["width"]
]
print(file_path_list)
输出:
['/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg', '/2Lgb7oPgqVpJmruRoH7Rs2yJH0n.jpg', '/6mLaG60xEQm6mw0Ga8TDFZsk0R1.jpg']
它通常有助于格式化一个复杂的数据结构,以便更好地了解各部分之间的关系。在这种情况下,它显示了您所关心的信息(高度
、宽度
,以及文件路径
)可以在dict中的海报
列表中的字典中找到这些信息,我们可以称之为海报
。利用这些信息,我们可以构建一个满足指定的高度
和宽度
关系的文件路径列表
例如:
poster_dict = {
"id": 142840,
"posters": [
{
"aspect_ratio": 0.703125,
"file_path": "/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg",
"height": 2048,
"iso_639_1": "en",
"vote_average": 5.312,
"vote_count": 1,
"width": 1440,
},
{
"aspect_ratio": 0.70703125,
"file_path": "/2Lgb7oPgqVpJmruRoH7Rs2yJH0n.jpg",
"height": 2048,
"iso_639_1": "en",
"vote_average": 5.172,
"vote_count": 1,
"width": 1448,
},
{
"aspect_ratio": 0.7068881289692233,
"file_path": "/6mLaG60xEQm6mw0Ga8TDFZsk0R1.jpg",
"height": 2047,
"iso_639_1": None,
"vote_average": 0.0,
"vote_count": 0,
"width": 1447,
},
],
}
file_path_list = [
one_poster["file_path"]
for one_poster in poster_dict["posters"]
if one_poster["height"] > one_poster["width"]
]
print(file_path_list)
输出:
['/4EFI8IdI7LOm9Q0BTT2FAdPwdeq.jpg', '/2Lgb7oPgqVpJmruRoH7Rs2yJH0n.jpg', '/6mLaG60xEQm6mw0Ga8TDFZsk0R1.jpg']
是的,谢谢是的,谢谢