简体   繁体   English

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

[英]Python for loop only returning last value of a dictionary

I'm trying to create a json dump with xyz coordinates in python, however the for loop im using to go trough different groups only returns the last group 我正在尝试在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)

The expected outcome is a JSON file with the coordinates of the placed points as following: 预期的结果是一个JSON文件,其放置点的坐标如下:

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

Every bracket for the placed point, the current out come is: 放置点的每个括号,当前得出的是:

[[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] [x,y,z] [x,y,z]。

Only 8 values since the size of the last group is 8 由于最后一组的大小为8,因此只有8个值

After trying some of your solutions I've ended up with this: 在尝试了一些解决方案后,我得出了以下结论:

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)

The output of print(data) is: print(data)的输出是:

[[17.006101346674598, -24.222496770994944, 95.14869919154683], [22.30318006424494, -21.376267007401097, 94.70820903177969], [-24.066693590510965, 21.205230021220736, 96.57992975278633], [-7.9541006992288885, 20.3986457061961, 103.06739548846576], [-28.291138300128495, 33.5422782651503, 99.22546203301508], [-40.61999270785583, 40.90496355476136, 90.2356807538543], [-39.293698815625135, 52.39636618754361, 96.72998820004932], [-28.29463915487483, 48.772250886978405, 102.25119515066885]] [[17.006101346674598,-24.222496770994944、95.14869919154683],[22.30318006424494,-21.376267007401097、94.70820903177969],[-24.066693590510965、21.205230021220736、96.57992975278633],[-7.9541006992288885、20.398-605.468。 40.90496355476136,90.2356807538543],[-39.293698815625135,52.39636618754361,96.72998820004932],[-28.29463915487483,48.772250886978405,102.25119515066885]]

In the for loop, you're overwriting data at every iteration with data = coords_data . for循环中,您每次迭代都会使用data = coords_data覆盖data If data is a list, then use data.append(coords_data) instead to add new data to it at each iteration. 如果data是一个列表,则在每次迭代时使用data.append(coords_data)来向其中添加新数据。 Note that you'll need to initialize it before the for loop with data = [] 请注意,您需要在for循环之前使用data = []对其进行初始化

Essentially: 实质上:

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

You overwrite your data variable after each iteration in your for loop, hence you only get that last iteration. for循环中的每次迭代之后,您都将覆盖data变量,因此只能得到最后一次迭代。 You'll need to initialize something to dump each iteration of the data into a "results" data of some sort: 您将需要初始化一些东西,以将数据的每次迭代转储到某种“结果”数据中:

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)

Your with block is outside the for loop so it's executed after loop finishes and has only access to the last element because that's the state with which the loop terminates. 您的with块位于for循环之外,因此它在循环结束后执行,并且只能访问最后一个元素,因为这是循环终止的状态。

But if you open a with inside your loop block everytime, you'll again get the same result, so you've to open it with append mode 'a+' 但是,如果每次都在循环块中打开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)

A even better way would be to run the loop inside the context manager. 更好的方法是在上下文管理器中运行循环。

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)

You need to add your coords_data to a list, where they will be remembered, and then write that list onto the file, right now data = coords_data only remembers the last value of coords_data and it writes that onto the file. 您需要将coords_data添加到一个列表中,将在其中将其记住,然后将该列表写入文件中,现在data = coords_data仅记住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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向 python 字典添加值,只返回最后一个值 - Adding values to python dictionary, only returning last value 为什么我的 Python 字典只返回最后一个值? - Why is my Python Dictionary only returning the last value? Python:在 for 循环之外打印字典仅打印最后一个值 - Python: Printing dictionary outside of for loop only prints last value 字典仅返回列表值中的最后一项 - Dictionary returning only the Last Item in List Value Python - For 循环不会用它的匹配值替换每个字典键......只合作字典中的最后一个键和值 - Python - For loop will not replace every dictionary key with it's matching value... Only cooperating for last key & value in dictionary "为什么我的 for 循环只返回最后一个值?" - Why is my for loop only returning last value? Django中的Python for循环在字典中返回随机值 - Python for loop in Django returning random value in dictionary 从Python的字典中仅返回一个值 - Returning only one value from a dictionary in Python 当我 go 通过 Python 中的循环时,只剩下最后一个键/值对添加到字典 - Only left with the last key/value pairs added to a dictionary when I go through a loop in Python 为什么我的合并字典只返回最后一个值? - Why is my merged dictionary only returning the last value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM