全局变量和局部变量在for循环的使用

imageloc字典作为全局变量,然后添加到全局的列表中,每次for循环都会将最新的元素改变之前for循环添加的元素。而imageloc字典作为局部变量,则不会影响。

c 复制代码
import numpy as np
originaljson = [
   {
      "joints_vis": [
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1
      ],
      "joints": [
         [
            804.0,
            711.0
         ],
         [
            816.0,
            510.0
         ],
         [
            908.0,
            438.0
         ],
         [
            1040.0,
            454.0
         ],
         [
            906.0,
            528.0
         ],
         [
            883.0,
            707.0
         ],
         [
            974.0,
            446.0
         ],
         [
            985.0,
            253.0
         ],
         [
            982.7591,
            235.9694
         ],
         [
            962.2409,
            80.0306
         ],
         [
            869.0,
            214.0
         ],
         [
            798.0,
            340.0
         ],
         [
            902.0,
            253.0
         ],
         [
            1067.0,
            253.0
         ],
         [
            1167.0,
            353.0
         ],
         [
            1142.0,
            478.0
         ]
      ],
      "image": "005808361.jpg",
      "scale": 4.718488,
      "center": [
         966.0,
         340.0
      ]
   },
   {
      "joints_vis": [
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1
      ],
      "joints": [
         [
            317.0,
            412.0
         ],
         [
            318.0,
            299.0
         ],
         [
            290.0,
            274.0
         ],
         [
            353.0,
            275.0
         ],
         [
            403.0,
            299.0
         ],
         [
            394.0,
            409.0
         ],
         [
            322.0,
            275.0
         ],
         [
            327.0,
            172.0
         ],
         [
            329.9945,
            162.1051
         ],
         [
            347.0055,
            105.8949
         ],
         [
            296.0,
            135.0
         ],
         [
            281.0,
            208.0
         ],
         [
            296.0,
            167.0
         ],
         [
            358.0,
            177.0
         ],
         [
            387.0,
            236.0
         ],
         [
            392.0,
            167.0
         ]
      ],
      "image": "052475642.jpg",
      "scale": 1.761835,
      "center": [
         316.0,
         220.0
      ]
   },
   {
      "joints_vis": [
         0,
         1,
         1,
         1,
         1,
         0,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1
      ],
      "joints": [
         [
            -1.0,
            -1.0
         ],
         [
            1033.0,
            649.0
         ],
         [
            1072.0,
            474.0
         ],
         [
            973.0,
            496.0
         ],
         [
            961.0,
            650.0
         ],
         [
            -1.0,
            -1.0
         ],
         [
            1023.0,
            485.0
         ],
         [
            1031.0,
            295.0
         ],
         [
            1026.998,
            281.6248
         ],
         [
            997.002,
            181.3752
         ],
         [
            988.0,
            294.0
         ],
         [
            1018.0,
            317.0
         ],
         [
            1070.0,
            290.0
         ],
         [
            991.0,
            300.0
         ],
         [
            912.0,
            345.0
         ],
         [
            842.0,
            330.0
         ]
      ],
      "image": "052475643.jpg",
      "scale": 3.139233,
      "center": [
         1030.0,
         396.0
      ]
   },
   {
      "joints_vis": [
         0,
         1,
         1,
         1,
         1,
         0,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1,
         1
      ],
      "joints": [
         [
            -1.0,
            -1.0
         ],
         [
            804.0,
            659.0
         ],
         [
            786.0,
            498.0
         ],
         [
            868.0,
            509.0
         ],
         [
            860.0,
            693.0
         ],
         [
            -1.0,
            -1.0
         ],
         [
            827.0,
            504.0
         ],
         [
            840.0,
            314.0
         ],
         [
            838.9079,
            308.9326
         ],
         [
            816.0921,
            203.0674
         ],
         [
            698.0,
            264.0
         ],
         [
            740.0,
            297.0
         ],
         [
            790.0,
            300.0
         ],
         [
            889.0,
            328.0
         ],
         [
            915.0,
            452.0
         ],
         [
            906.0,
            553.0
         ]
      ],
      "image": "004645041.jpg",
      "scale": 3.248877,
      "center": [
         809.0,
         403.0
      ]
   }
]


newjson = []

# imageloc = {}
for i in range(len(originaljson)):
   imageloc = {}
   box = []
   imagename = originaljson[i]["image"].split('.')[0]

   box.append(1)
   box.append(2)
   box.append(3)
   box.append(4)

   imageloc[imagename] = box
   newjson.append(imageloc)
相关推荐
喜欢猪猪1 分钟前
Django:从入门到精通
后端·python·django
糖豆豆今天也要努力鸭7 分钟前
torch.__version__的torch版本和conda list的torch版本不一致
linux·pytorch·python·深度学习·conda·torch
何大春23 分钟前
【弱监督语义分割】Self-supervised Image-specific Prototype Exploration for WSSS 论文阅读
论文阅读·人工智能·python·深度学习·论文笔记·原型模式
在下不上天31 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
SEVEN-YEARS35 分钟前
深入理解TensorFlow中的形状处理函数
人工智能·python·tensorflow
EterNity_TiMe_40 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
Suyuoa1 小时前
附录2-pytorch yolov5目标检测
python·深度学习·yolo
好看资源平台2 小时前
网络爬虫——综合实战项目:多平台房源信息采集与分析系统
爬虫·python
进击的六角龙2 小时前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂2 小时前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python