Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 我想从嵌套字典中提取特定的键、值?_Python_Dictionary - Fatal编程技术网

Python 我想从嵌套字典中提取特定的键、值?

Python 我想从嵌套字典中提取特定的键、值?,python,dictionary,Python,Dictionary,从这个字典中,我只想从整个字典中提取“id”和“file_name” 我尝试了一些方法,但每次都得到一个空列表。。。 如何提取?请纠正我 {'images': [{'id': 124, 'file_name': '124.jpg', 'height': 800, 'width': 800, 'license': 1}, {'id': 125, 'file_name': '125.jpg', 'height': 800, 'width': 800,

从这个字典中,我只想从整个字典中提取“id”和“file_name” 我尝试了一些方法,但每次都得到一个空列表。。。 如何提取?请纠正我

{'images': [{'id': 124,
   'file_name': '124.jpg',
   'height': 800,
   'width': 800,
   'license': 1},
  {'id': 125,
   'file_name': '125.jpg',
   'height': 800,
   'width': 800,
   'license': 1},
  {'id': 126,
   'file_name': '126.jpg',
   'height': 800,
   'width': 800,
   'license': 1},....

要使用
dict
wise值获取
id
file\u name
值,请使用

data = {'images': [{'id': 124,
   'file_name': '124.jpg',
   'height': 800,
   'width': 800,
   'license': 1},
  {'id': 125,
   'file_name': '125.jpg',
   'height': 800,
   'width': 800,
   'license': 1},
  {'id': 126,
   'file_name': '126.jpg',
   'height': 800,
   'width': 800,
   'license': 1}] }


files = { v['id']: v['file_name'] for v in data['images'] } 
print( files )
类似地,您可以将其设置为
元组
嵌套
列表

res = [{d['id']:d['file_name']} for d in data['images']]
res = [{d['id']:d['file_name']} for d in data['images']]
res = [[d['id'],d['file_name']] for d in data['images']]

# Output
# [{124: '124.jpg'}, {125: '125.jpg'}, {126: '126.jpg'}]
# [[124, '124.jpg'], [125, '125.jpg'], [126, '126.jpg']]