python - 将字典项插入字典列表
问题描述
我有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'}]
我有迭代器,它假设在从网络获取它之后在每个图像记录下添加本地 URL(获取部分工作正常)。因此,我使用以下代码将本地 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
想法是以下列方式扩展 dict 项目,这也解释了我的代码中的数字 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}
但这给了我错误:
TypeError:“builtin_function_or_method”对象不可下标
解决方案
很可能这是您的想法:
adImageList[i]['Image_thumb_100x75_local_path_thumb']=local_path_thumb
这会将键添加'Image_thumb_100x75_local_path_thumb'
到i
列表中的第 th 个字典并将其值设置为local_path_thumb
。1,3,5 的用途还不清楚。
推荐阅读
- h2o - 仅选择每个 h2o 数据帧 group_by 组中的第一行(用于合并)?
- php - 如何使用 PHP 和 DOMDocument 生成具有页面本地化版本的 Google 站点地图
- postgresql - 将本地数据库迁移到 AWS Aurora
- php - 如何使用 PHP 从 URL 获取 JSON 数据?我收到身份验证失败
- mysql - 如何将shell脚本变量作为OUT参数传递给mysql存储过程?
- javascript - Node.js Handlebars {{object}} 不在 {{each}} 循环内渲染,而是在循环外渲染(以及其他 {{object_names}} 在循环内渲染)
- angular - 如何在 Angular Element 中使用 Angular 组件?
- node.js - UnhandledPromiseRejectionWarning:未处理的承诺拒绝。MongoNetworkError:无法连接到服务器 [localhost:27017]
- javascript - VSCode 中的 TypeScript 错误:“无法写入文件‘x’,因为它会覆盖输入文件。”
- python - 字典循环获取每个值