Python for循环仅返回字典的最后一个值

时间:2019-05-02 10:36:38

标签: python json python-3.x dictionary

我正在尝试在python中使用xyz坐标创建json转储,但是用于遍历不同组的for循环即时消息只会返回最后一个组

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   for grp_str in self.group_strings:
       coords_data = self.point_dict[grp_str]['Coords']
       data = coords_data

   with open("data_file.json", "w") as write_file:
       json.dump(data, write_file)

预期结果是一个JSON文件,其放置点的坐标如下:

[[[x,y,z] [x,y,z] [x,y,z] [x,y,z] [x,y,z] [x,y,z]等... ]。

放置点的每个括号中,当前出现的是:

[[x,y,z] [x,y,z] [x,y,z] [x,y,z] [x,y,z] [x,y,z] [x,y ,z] [x,y,z]]。

由于最后一组的大小为8,因此只有8个值

尝试了一些解决方案后,我得出了以下结论:

data = []
if reply == QMessageBox.Yes:
    for grp_str in self.group_strings:
        data.append(self.point_dict[grp_str]['Coords'])

        with open("data_file.json", "w") as write_file:
            json.dump(data, write_file)

print(data)的输出是:

  

[[[17.006101346674598,-24.222496770994944,95.14869919154683],[22.30318006424494,-21.376267007401097,94.70820903177969],[-24.066693590510965,21.205230021220736,96.57992975278633],[-7.9541006992288,308.292.288。 -40.61999270785583、40.90496355476136、90.2356807538543],[-39.293698815625135、52.39636618754361、96.72998820004932],[-28.29463915487483、48.772250886978405、102.25119515066885]]

4 个答案:

答案 0 :(得分:3)

for循环中,您每次使用data覆盖data = coords_data。如果data是一个列表,请在每次迭代时使用data.append(coords_data)向其中添加新数据。请注意,您需要在for的{​​{1}}循环之前对其进行初始化

本质上:

data = []

答案 1 :(得分:1)

data循环中的每次迭代之后,您都将覆盖for变量,因此,您只会得到最后一次迭代。您需要初始化一些东西,以将数据的每次迭代转储到某种“结果”数据中:

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

data = []
if reply == QMessageBox.Yes:
   for grp_str in self.group_strings:
       data.append(self.point_dict[grp_str]['Coords'])

   with open("data_file.json", "w") as write_file:
       json.dump(data, write_file)

答案 2 :(得分:1)

您的with块位于for循环之外,因此它在循环结束后执行,并且只能访问最后一个元素,因为这是循环终止的状态。

但是,如果每次在循环块中打开with,都会再次得到相同的结果,因此必须使用附加模式'a +'来打开它

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   for grp_str in self.group_strings:
       coords_data = self.point_dict[grp_str]['Coords']
       data = coords_data
       # with is now inside the for loop
       with open("data_file.json", "a+") as write_file:
           json.dump(data, write_file)

更好的方法是在上下文管理器中运行循环。

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   with open("data_file.json", "w") as write_file:
       for grp_str in self.group_strings:
           coords_data = self.point_dict[grp_str]['Coords']
           data = coords_data
           json.dump(data, write_file)

答案 3 :(得分:0)

您需要将coords_data添加到一个列表中,将在其中记住这些列表,然后将该列表写入文件中,现在data = coords_data仅记住coords_data的最后一个值,并写入该值到文件上。

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   datas = []
   for grp_str in self.group_strings:
       #Append all values to a list
       datas.append(self.point_dict[grp_str]['Coords'])

   #Write that list to file
   with open("data_file.json", "w") as write_file:
       json.dump(datas, write_file)
相关问题