简体   繁体   English

将字典项目插入字典列表中

[英]Insert dictionary item into list of dictionaries

I have a list adImageList of dictionary items in following form: 我有以下形式的字典项的列表adImageList

[{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_174707044_thumb.jpg',
  'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_174707044_hoved.jpg',
  'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_174707044.jpg'},
 {'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_1136648194_thumb.jpg',
  'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_1136648194_hoved.jpg',
  'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_1136648194.jpg'},
 {'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_400613427_thumb.jpg',
  'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_400613427_hoved.jpg',
  'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_400613427.jpg'}]

I have iterator which suppose to add local URL under each image record after fetching it from web (fetching part works ok). 我有一个迭代器,它假定在从网络中获取本地图像后在每个图像记录下添加本地URL(获取部分可以正常工作)。 So I'm using following code to append local URL to existing dictionary items: 因此,我正在使用以下代码将本地URL附加到现有字典项中:

 for i, d in enumerate(adImageList):
                file_name_thumb = '0{}_{}_{}'.format(i, page_title,'_thumb_100x75.jpg')
                urllib.request.urlretrieve(d['Image_thumb_100x75'], file_name_thumb)
                local_path_thumb = dir_path+file_name_thumb
                adImageList.insert[i](1,{'Image_thumb_100x75_local_path_thumb':local_path_thumb}) # not working

                file_name_hoved = '0{}_{}_{}'.format(i, page_title,'_hoved_400x300.jpg')
                urllib.request.urlretrieve(d['Image_hoved_400x300'], file_name_hoved)
                local_path_hoved = dir_path+file_name_hoved
                adImageList.insert[i](3,{'Image_hoved_400x300_local_path_hoved':local_path_hoved}) # not working

                file_name_full = '0{}_{}_{}'.format(i, page_title,'_full_800x600.jpg')
                urllib.request.urlretrieve(d['Image_full_800x600'], file_name_full)
                local_path_full = dir_path+file_name_full
                adImageList.insert[i](5,{'Image_full_800x600_local_path_full':local_path_full}) # not working

Idea is to extend dict items in following manner which also explains numbers 1,3 and 5 in my code 想法是通过以下方式扩展字典项, 这也解释了我的代码中的数字1,3和5

{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_174707044_thumb.jpg',
  'Image_thumb_100x75_local_path_thumb':local_path_thumb #1,
  'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_174707044_hoved.jpg',
  'Image_hoved_400x300_local_path_hoved':local_path_hoved #3
  'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_174707044.jpg',
  'Image_full_800x600_local_path_full':local_path_full #5}

But it's giving me error: 但这给了我错误:

TypeError: 'builtin_function_or_method' object is not subscriptable TypeError:“ builtin_function_or_method”对象不可下标

Most likely here's what you had in mind: 这很可能是您的想法:

adImageList[i]['Image_thumb_100x75_local_path_thumb']=local_path_thumb

This adds key 'Image_thumb_100x75_local_path_thumb' to the i th dictionary on the list and sets its value to local_path_thumb . 这会将键'Image_thumb_100x75_local_path_thumb'添加到列表中的第i个字典,并将其值设置为local_path_thumb The purpose of 1,3,5 is still unclear. 1,3,5的目的仍然不清楚。

python stack traces give line numbers for a reason, but my guess is this line: python堆栈跟踪给出了行号是有原因的,但是我的猜测是这一行:

adImageList.insert[i]

insert is a method 插入是一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM