代码之家  ›  专栏  ›  技术社区  ›  Hrvoje

将词典项插入词典列表

  •  -1
  • Hrvoje  · 技术社区  · 6 年前

    我有一张单子 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'}]
    

    我有一个迭代器,它假设在从web获取图像记录后,在每个图像记录下添加本地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
    

    这也解释了我代码中的数字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}
    

    但这给了我一个错误:

    2 回复  |  直到 6 年前
        1
  •  1
  •   DYZ    6 年前

    很可能你的想法是:

    adImageList[i]['Image_thumb_100x75_local_path_thumb']=local_path_thumb
    

    这增加了关键点 'Image_thumb_100x75_local_path_thumb' i 并将其值设置为 local_path_thumb . 1,3,5的目的还不清楚。

        2
  •  0
  •   booleys1012    6 年前

    python堆栈跟踪给出行号是有原因的,但我猜是这样的:

    adImageList.insert[i]