将每个点的Y坐标增加120
# 将每个点的 y 坐标增加 120points=[[(570,510),(652,507)],[(667,492),(667,125)],
[(697,125),(702,492)],[(732,492),(732,125)],
[(897,110),(747,110)],[(897,140),(782,143)],
[(767,157),(767,525)],[(752,540),(570,540)]]
points2 = [(570,525), (652,492), (682,125), (747,125),
(897,125), (782,158), (752,525), (717,492)]
adjusted_points = [[(x, y + 120) for x, y in sublist] for sublist in points]
print(adjusted_points)
adjusted_points2 = [(x, y + 120) for x, y in points2]
print(adjusted_points2) 问题解答
你的代码存在语法错误和格式问题,修正后完整代码如下:
# 原始坐标列表(示例数据)
points = [
[, ],
[, ]
]
# 对每个点的y坐标增加120
adjusted_points = [[ for x, y in sublist] for sublist in points]
print(adjusted_points)# 输出 [[, ], [, ]]
# 如果是单层列表的情况(示例)
points2 = [, ]
adjusted_points2 = [ for x, y in points2]
print(adjusted_points2)# 输出 [, ]
关键点说明
[*]原始数据需要是合法的列表嵌套结构
[*]使用嵌套列表推导式处理多维列表
[*]使用元组解包获取x,y坐标:
[*]根据列表层级调整循环次数:
三层列表 → 三层循环
两层列表 → 两层循环
如果还有疑问,可以补充具体数据结构样例,我会给出更针对性的解答~
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 代码没有问题啊? 循环遍历 配合数组索引就完成了
页:
[1]