全局变量和局部变量在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)
相关推荐
梦想画家2 分钟前
Python Polars快速入门指南:LazyFrames
python·数据分析·polars
程序猿000001号15 分钟前
使用Python的Seaborn库进行数据可视化
开发语言·python·信息可视化
API快乐传递者24 分钟前
Python爬虫获取淘宝详情接口详细解析
开发语言·爬虫·python
公众号Codewar原创作者26 分钟前
R数据分析:工具变量回归的做法和解释,实例解析
开发语言·人工智能·python
FL162386312931 分钟前
python版本的Selenium的下载及chrome环境搭建和简单使用
chrome·python·selenium
巫师不要去魔法部乱说35 分钟前
PyCharm专项训练5 最短路径算法
python·算法·pycharm
Chloe.Zz42 分钟前
Python基础知识回顾
python
骑个小蜗牛1 小时前
Python 标准库:random——随机数
python
Trouvaille ~1 小时前
【机器学习】从流动到恒常,无穷中归一:积分的数学诗意
人工智能·python·机器学习·ai·数据分析·matplotlib·微积分
是十一月末1 小时前
Opencv实现图像的腐蚀、膨胀及开、闭运算
人工智能·python·opencv·计算机视觉