使用for循环分配字典项时出现问题

2024-03-29 05:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

head = {
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "id":4,
         "geometry":{
            "type":"Point",
            "coordinates":[
               -0.1044968729335983,
               51.52014078866054
            ]
         },
         "properties":{
            "description":"Фаррингдон-стрит",
            "iconCaption":"Фаррингдон-стрит",
            "marker-color":"#b51eff"
         }
      }
   ]
}

我在列表中的第一项总是这个(下面)

head['features'][0]['geometry']['coordinates'][0] = long
head['features'][0]['geometry']['coordinates'][1] = lat



zone = {
   "type":"Feature",
   "id":0,
   "geometry":{
      "type":"Polygon",
      "coordinates":[]
   },
   "properties":{
      "description":"Вокзал Паддингтон",
      "fill":"#ff931e",
      "fill-opacity":0.3,
      "stroke":"#e6761b",
      "stroke-width":"2",
      "stroke-opacity":0.9
   }
}

所以这是我的密码。它识别出我需要的地址的数量,并将那么多的条目添加到列表中

for i in range (0, len(lst)):
   head['features'].append(zone)

不知何故,我得到了坐标并将其存储在列表lst[]中。当我试图用代码替换它时:

for k in range (0, (len (lst)) ):
  head['features'][k+1]['geometry']['coordinates'] = lst[k]['data-points']

它用列表lst[]中的最后一项替换“coordinate”中的所有项。 奇怪的问题。但当我在pythonshell中手动执行时,它就可以工作了。你知道吗


Tags: idzone列表stroketypedescriptionpropertiesfill