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

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

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)

你可能感兴趣的:(python)