全局变量和局部变量在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)
相关推荐
查理零世39 分钟前
保姆级讲解 python之zip()方法实现矩阵行列转置
python·算法·矩阵
刀客1231 小时前
python3+TensorFlow 2.x(四)反向传播
人工智能·python·tensorflow
sysu632 小时前
95.不同的二叉搜索树Ⅱ python
开发语言·数据结构·python·算法·leetcode·面试·深度优先
SsummerC2 小时前
【leetcode100】从前序与中序遍历序列构造二叉树
python·算法·leetcode
陌北v13 小时前
PyTorch广告点击率预测(CTR)利用深度学习提升广告效果
人工智能·pytorch·python·深度学习·ctr
Мартин.3 小时前
[Meachines] [Easy] Bashed PHP Bash+Python计划任务权限提升
python·php·bash
码界筑梦坊3 小时前
基于Flask的旅游系统的设计与实现
python·flask·毕业设计·旅游
辞落山4 小时前
自定义数据集使用scikit-learn中的包实现线性回归方法对其进行拟合
python·线性回归·scikit-learn
Allen200005 小时前
wow-agent---task4 MetaGPT初体验
人工智能·python·pygame
源代码杀手8 小时前
【以音频软件FFmpeg为例】通过Python脚本将软件路径添加到Windows系统环境变量中的实现与原理分析
windows·python·音视频