gpt4 book ai didi

python - 如何在python中访问列表内的坐标?

转载 作者:行者123 更新时间:2023-12-04 10:16:57 24 4
gpt4 key购买 nike

我有一个包含列表中多边形坐标的列表。如何访问此列表中存在的 x 和 y 点?基本上,我必须使用这些点和情节。我必须从以下位置提取 all_points_x 和 all_points_y:

[{'shape_attributes':
{'name': 'polygon',
'all_points_x': [35, 28, 27, 31, 40, 51, 62, 72, 74, 71, 65, 57, 41],
'all_points_y': [74, 55, 32, 16, 4, 6, 12, 35, 56, 74, 83, 86, 81]},
'region_attributes': {}},
None,
{'shape_attributes':
{'name': 'polygon',
'all_points_x': [6, 16, 24, 44, 69, 77, 81, 82, 80, 76, 69, 62, 51, 26, 9, 7],
'all_points_y': [85, 77, 78, 83, 92, 100, 106, 115, 118, 120, 122, 125, 126, 112, 98, 92]},
'region_attributes': {}}]

最佳答案

一种方法是遍历您的列表,其中包含列表为 x 的对象字典。和 y坐标,您可以提取坐标并将它们以元组形式放入列表中,例如 (x,y) :

shapesCordinates = []
for shape in shapes:
if shape is not None:
x_cor = shape['shape_attributes']['all_points_x']
y_cor = shape['shape_attributes']['all_points_y']
for x, y in zip(x_cor, y_cor):
shapesCordinates.append((x, y))
print(shapesCordinates)

输出:
[(35, 74), (28, 55), (27, 32), (31, 16), (40, 4), (51, 6), (62, 12), (72, 35), (74, 56), (71, 74), (65, 83), (57, 86), (41, 81), (6, 85), (16, 77), (24, 78), (44, 83), (69, 92), (77, 100), (81, 106), (82, 115), (80, 118), (76, 120), (69, 122), (62, 125), (51, 126), (26, 112), (9, 98), (7, 92)]

关于python - 如何在python中访问列表内的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61024400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com