Home > other >  Generate matrix and save it in list
Generate matrix and save it in list

Time:11-18

I want to create a list like the following, based on a starting point (x, y):

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

My actual starting point is (71, 180) and

x_distance = 105   
y_distance = 111  

My expected output is (6x5) format:

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

I've tried the following code:

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)

Output (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]]

I've also tried the following code:

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

The output is close, but the y-maximum is 624 and not 735 as expected:

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

CodePudding user response:

You could use the full range of range options:

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

Result:

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

CodePudding user response:

YOU ARE CLOSE!!

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)        
  • OUTPUT:
[[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]]
  • Related