如何基于起始点与间隔生成指定6×5格式的坐标列表?
需求说明
需要基于起始点(71, 180)生成如下格式的坐标列表:
[[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]]
给定参数:
x_distance = 105 y_distance = 111
需生成6列5行的坐标列表,预期输出示例:
[[71,180], [176,180], [281,180], [386,180], [491,180], [596,180], [71,291], [176,291], [281,291], [386,291], [491,291], [596,291], [71,402], [176,402], [281,402], [386,402], [491,402], [596,402], [71,513], [176,513], [281,513], [386,513], [491,513], [596,513], [71,624], [176,624], [281,624], [386,624], [491,624], [596,624]]
尝试的代码及问题
第一段代码
first_xy_x = 71 first_xy_y = 180 x_distance = 150 y_distance = 111 predict_xy_list = [] tem_predict_xy_list = [ ] tem_predict_xy_list.append(first_xy_x) tem_predict_xy_list.append(first_xy_y) predict_xy_list.append(tem_predict_xy_list) last_x = first_xy_x last_y = first_xy_y for x in range(5): tem_predict_xy_list = [ ] last_x = int(last_x) + int(x_distance) for y in range(5): tem_predict_xy_list = [ ] last_y = int(last_y) + int(y_distance) tem_predict_xy_list.append(last_x) tem_predict_xy_list.append(last_y) predict_xy_list.append(tem_predict_xy_list)
输出结果:
[[71, 180], [176, 291], [176, 402], [176, 513], [176, 624], [176, 735], [281, 846], [281, 957], [281, 1068], [281, 1179], [281, 1290], [386, 1401], [386, 1512], [386, 1623], [386, 1734], [386, 1845], [491, 1956], [491, 2067], [491, 2178], [491, 2289], [491, 2400], [596, 2511], [596, 2622], [596, 2733], [596, 2844], [596, 2955], [701, 3066], [701, 3177], [701, 3288], [701, 3399], [701, 3510]]
问题:坐标的y值持续累加,不符合每行y值固定的要求,且x_distance误用为150。
第二段代码
first_xy_x = 71 first_xy_y = 180 x_distance = 150 y_distance = 111 predict_xy_list = [] tem_predict_xy_list = [ ] tem_predict_xy_list.append(first_xy_x) tem_predict_xy_list.append(first_xy_y) predict_xy_list.append(tem_predict_xy_list) last_x = first_xy_x last_y = first_xy_y tem_predict_xy_list = [ ] for y in range(4): tem_predict_xy_list = [ ] last_y = int(last_y) + int(y_distance) tem_predict_xy_list.append(first_xy_x) tem_predict_xy_list.append(last_y) predict_xy_list.append(tem_predict_xy_list) print(predict_xy_list) print(len(predict_xy_list)) print("= = = = = ") for x in range(5): tem_predict_xy_list = [ ] last_x = int(last_x) + int(x_distance) last_y = first_xy_y for y in range(5): tem_predict_xy_list = [ ] last_y = int(last_y) + int(y_distance) tem_predict_xy_list.append(last_x) tem_predict_xy_list.append(last_y) predict_xy_list.append(tem_predict_xy_list) print(predict_xy_list) print(len(predict_xy_list))
输出结果:
[[71, 180], [71, 291], [71, 402], [71, 513], [71, 624], [221, 291], [221, 402], [221, 513], [221, 624], [221, 735], [371, 291], [371, 402], [371, 513], [371, 624], [371, 735], [521, 291], [521, 402], [521, 513], [521, 624], [521, 735], [671, 291], [671, 402], [671, 513], [671, 624], [671, 735], [821, 291], [821, 402], [821, 513], [821, 624], [821, 735]]
问题:输出接近预期,但首列y最大值仅为624,且x_distance误用为150,整体行列顺序不符合要求。
请求帮助
请修正代码,生成符合预期的6×5格式坐标列表。
内容的提问来源于stack exchange,提问作者user20384021
相关产品推荐
相关产品推荐

