Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—如何从大量dict列表中获取特定值?_Python_List_Dictionary_For Loop - Fatal编程技术网

Python—如何从大量dict列表中获取特定值?

Python—如何从大量dict列表中获取特定值?,python,list,dictionary,for-loop,Python,List,Dictionary,For Loop,我有一个很大的dict列表,但我只想从dict列表中获得一个特定的信息 如何从dict列表中的键standart:('standard':{'width':640,'height':480,'url':'TheURL here'})获取值 {'snippet':{'videoOwnerChannelId':'UCG8rbF3g2AMX70yOd8vqIZg','channelTitle':'Logan Paul','videoOwnerChannelTitle':'Logan Paul','pl

我有一个很大的dict列表,但我只想从dict列表中获得一个特定的信息

如何从dict列表中的键standart:('standard':{'width':640,'height':480,'url':'TheURL here'})获取值


{'snippet':{'videoOwnerChannelId':'UCG8rbF3g2AMX70yOd8vqIZg','channelTitle':'Logan Paul','videoOwnerChannelTitle':'Logan Paul','playlid':'plh3cbjrcytxigl0jpljjjjvm60s6oenaa','description':'加入这场运动,做一个特立独行的人► https://ShopLoganPaul.com/\在大家面前…\n订阅每日视频日志!►\nWatch先前的Vlog► https://youtu.be/kOGkeS4Jbkg\n\n将我添加到:\n标签:https://www.instagram.com/LoganPaul/\恩特维特:https://twitter.com/LoganPaul\n\n我是一个住在洛杉矶的23岁男童。这就是我的生活\nhttps://www.youtube.com/LoganPaulVlogs“,”channelId“:”UCG8rbF3g2AMX70yOd8vqIZg“,”title“:”我一生中最尴尬的时刻-巧克力故事S EP.5,“resourceId:{'videoId:'ft6FthsVWaY”,“kind:'youtube#video'},'缩略图':{'medium':{'width':320',height':180',url:'https://i.ytimg.com/vi/ft6FthsVWaY/mqdefault.jpg“},'标准':{'width':640,'高度]:480,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/sddefault.jpg“},‘高’:{'width':480,'height':360,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/hqdefault.jpg“},'默认值':{'width':120,'height':90,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/default.jpg“},'maxres':{'width':1280,'height':720,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/maxresdefault.jpg“}”,位置:4,“发布日期”:“2019-03-19T02:00:20Z”,'id':'uexim2ncaljdevrueelhbdbkcexqsjf2btywuzvzw5hys4wota3otzbnzvemtuzotmy','etag':'uuu0suemm-gC0bcXTF9v1pFV9ZJY','kind':'youtube#playlitem'}

在您的示例中没有词典列表,即
[{},{},{},]
而不是嵌套字典。如果确定所有键都必须存在,则可以按如下方式访问该值:

std_thumb = myDict['snippet']['thumbnails']['standard']
width = std_thum['width']
height = std_thum['height']
url = std_thum['url']
如果您确定所有嵌套字典中只有一个称为“standard”的键,那么以下内容也适用,摘自:

如果这些键始终处于嵌套的顶层,Ahmadreza的答案当然是合适的,但在您的示例中并非如此。我的第一个示例是索引字典的标准方法,第二个示例是递归查找键

另外,您不需要在eachDict.keys()中调用
if'standard'
您只需调用
if standard in eachDict
,因为“in”关键字已经只引用了键

p.p.S.您可能会发现使用json库可视化复杂字典很有用;下面将为您缩进它:

import json
print(json.dumps(myDict, indent=4))

在您的示例中,没有字典列表,即
[{},{},…]
,而是嵌套字典。如果您确定所有键都必须存在,则可以按如下方式访问该值:

std_thumb = myDict['snippet']['thumbnails']['standard']
width = std_thum['width']
height = std_thum['height']
url = std_thum['url']
如果您确定所有嵌套字典中只有一个称为“standard”的键,那么以下内容也适用,摘自:

如果这些键始终处于嵌套的顶层,Ahmadreza的答案当然是合适的,但在您的示例中并非如此。我的第一个示例是索引字典的标准方法,第二个示例是递归查找键

另外,您不需要在eachDict.keys()中调用
if'standard'
您只需调用
if standard in eachDict
,因为“in”关键字已经只引用了键

p.p.S.您可能会发现使用json库可视化复杂字典很有用;下面将为您缩进它:

import json
print(json.dumps(myDict, indent=4))

假设您的字典位于数据变量中

  • 如果您只需要提取这些数据一次,您可以使用:
data['snippet']['thumbnails']['standard']
  • 否则,您可以使用:
对于数据中的项:
如果项==“代码段”:
有关数据[项目]中的信息:
如果信息==“缩略图”:
打印(数据[项目][信息][标准])
>>>输出
{'width':640,'height':480,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/sddefault.jpg'}

假设您的字典位于数据变量中

  • 如果您只需要提取这些数据一次,您可以使用:
data['snippet']['thumbnails']['standard']
  • 否则,您可以使用:
对于数据中的项:
如果项==“代码段”:
有关数据[项目]中的信息:
如果信息==“缩略图”:
打印(数据[项目][信息][标准])
>>>输出
{'width':640,'height':480,'url':'https://i.ytimg.com/vi/ft6FthsVWaY/sddefault.jpg'}

我不明白…你想得到什么信息?dict缩略图中的信息>>标准你是说
打印(d[“snippet”][“thumbnails”][“standard”])
?@AndrejKesely yesi不明白…你想得到什么信息?dict缩略图中的信息>>标准你是指
打印(d)[“snippet”][“thumbnails”][“standard”]
?@AndrejKesely yessI收到错误-->std_thum=OurList['snippet']['thumbnails']['standard']TypeError:列表索引必须是整数或片,而不是要在dict上调用的标准,因此myDict[key1][key2]等等。如果它是一个列表,你可以说“std_thumb\u list=[d['snippet snippet']['standard']对于dict列表中的d,我得到了错误-->std\u thum=OurList['snippet']['thumbnails']['standard']TypeError:列表索引必须是整数或切片,而不是要在dict上调用的标准,因此myDict[key1][key2]等等。如果是一个列表,你可以说“std\u thumbu list=[d['snippet']['thumbnails']['standard']谢谢你。它工作得很好。谢谢。它工作得很好