全局变量和局部变量在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)
相关推荐
FreakStudio2 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21362 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
_.Switch3 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技3 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )4 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温4 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学4 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹4 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt
羊小猪~~5 小时前
深度学习项目----用LSTM模型预测股价(包含LSTM网络简介,代码数据均可下载)
pytorch·python·rnn·深度学习·机器学习·数据分析·lstm
Marst Code5 小时前
(Django)初步使用
后端·python·django