python-3.x 生成矩阵并保存在列表中

d8tt03nd  于 2022-11-19  发布在  Python
关注(0)|答案(2)|浏览(159)

我想基于(x, y)创建一个如下列表:

[[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]]

我的实际出发点是(71, 180)

x_distance = 105   
y_distance = 111

我预期的输出为(6x5)格式:

[[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)

输出(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]]

我也尝试过以下代码:

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))

输出接近,但y最大值为624,而不是预期的735:

[[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]]
kxe2p93d

kxe2p93d1#

您可以使用range选项的全部范围:

start_x, start_y = 71, 180
dist_x, dist_y = 105, 111
res = [
    [x, y]
    for y in range(start_y, start_y + 5 * dist_y, dist_y)
    for x in range(start_x, start_x + 6 * dist_x, dist_x)
]

结果:

[[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]]
kr98yfug

kr98yfug2#

您已接近!!

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)

for x in range(5):
    tem_predict_xy_list = [ ] 
    last_x = int(last_x) + int(x_distance)
    tem_predict_xy_list.append(last_x)
    tem_predict_xy_list.append(first_xy_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(4):
        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))

sorted_predict_xy_list = (sorted(predict_xy_list , key=lambda k: [k[1], k[0]]))
print(sorted_predict_xy_list)
  • 输出:
[[71, 180], [71, 291], [71, 402], [71, 513], [71, 624], [221, 180], [371, 180], [521, 180], [671, 180], [821, 180]]
10
= = = = = 
[[71, 180], [71, 291], [71, 402], [71, 513], [71, 624], [221, 180], [371, 180], [521, 180], [671, 180], [821, 180], [971, 291], [971, 402], [971, 513], [971, 624], [1121, 291], [1121, 402], [1121, 513], [1121, 624], [1271, 291], [1271, 402], [1271, 513], [1271, 624], [1421, 291], [1421, 402], [1421, 513], [1421, 624], [1571, 291], [1571, 402], [1571, 513], [1571, 624]]
30
[[71, 180], [221, 180], [371, 180], [521, 180], [671, 180], [821, 180], [71, 291], [971, 291], [1121, 291], [1271, 291], [1421, 291], [1571, 291], [71, 402], [971, 402], [1121, 402], [1271, 402], [1421, 402], [1571, 402], [71, 513], [971, 513], [1121, 513], [1271, 513], [1421, 513], [1571, 513], [71, 624], [971, 624], [1121, 624], [1271, 624], [1421, 624], [1571, 624]]

相关问题