Generate matrix and save it in list - python

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

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

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

declare 2d array without initializing a size and append operation (Python)

def XY(a):
XY = np.array([])
for i in a:
if (i[1] > 300) and (i[1] < 800) and (i[0] < 375) and (i[0] > 310):
XY = np.append(XY, [i])
return XY
a = array([[324, 664], [324, 665], [324, 666], [324, 667], [324, 668], [324, 669], [324, 670], [324, 671], [325, 664], [325, 665], [325, 666], [325, 667], [325, 668], [325, 669], [325, 670], [325, 671], [326, 664], [326, 665], [326, 666], [326, 667], [326, 668], [326, 669], [326, 670], [326, 671], [327, 664], [327, 665], [327, 666], [327, 667]])
Shape of the array 'a' is (num ,2). I want return array to be in the same shape like (some number, 2). But this function returns something like that (some number,1). How can I fix it? Thanks in advance.
You need to append to ndarray with the same shape on axis 0
def XY(a):
XY = np.empty([0, 2], dtype=int)
for i in a:
if 300 < i[1] < 800 and 310 < i[0] < 375:
XY = np.append(XY, [i], axis=0)
return XY
arr = XY(np.array([[324, 664], [324, 665], [324, 666], [324, 667], [324, 668], [324, 669], [324, 670], [324, 671], [325, 664], [325, 665], [325, 666], [325, 667], [325, 668], [325, 669], [325, 670], [325, 671], [326, 664], [326, 665], [326, 666], [326, 667], [326, 668], [326, 669], [326, 670], [326, 671], [327, 664], [327, 665], [327, 666], [327, 667]]))
print(arr.shape)
print(arr)
Output
(28, 2)
[[324 664]
[324 665]
[324 666]
[324 667]
[324 668]
[324 669]
[324 670]
[324 671]
[325 664]
[325 665]
[325 666]
[325 667]
[325 668]
[325 669]
[325 670]
[325 671]
[326 664]
[326 665]
[326 666]
[326 667]
[326 668]
[326 669]
[326 670]
[326 671]
[327 664]
[327 665]
[327 666]
[327 667]]

How to convert a pandas dataframe column to an image array i.e. a numpy array with shape (n,n) in Python?

Suppose my dataframe has 750 rows in a column and I want to convert that column to an image array of (20,20) numpy array. How to do that?
EDIT1: I want to use the array for ax.contourf (x,y,z) as z. I got x,y by doing x,y=np.meshgrid(df3.x,df.y) now I want to convert another column to an (n,n) array to vary the colors inside the contours using the z parameter.
EDIT2:x,y,z Data to plot contour plot
Given the fact that you have x and y as columns already, I would use pivot_table like this :
df = pd.DataFrame(
columns=["z", "x", "y"],
data=[
[-0.00222, 38, 46.1], [-0.00374, 30.5, 68.4], [0.001043, 36, 86.9], [0.003473, 52.9, 95.3], [0.001382, 50.6, 80.3], [-0.00486, 37, 92.7], [-0.00016, 29.8, 69.5], [0.001368, 34.7, 27.2], [-0.0016, 37.5, 63], [-0.00181, 45.5, 92], [0.003635, 53.6, 82.4], [-0.00363, 46.7, 91.1], [0.002253, 54.1, 97.8], [-0.00089, 43.8, 87], [-0.00115, 41, 62.7], [-0.001, 47.5, 23.2], [0.001332, 61.2, 4.7], [-0.00361, 45.9, 13.9], [0, 47.4, 32], [0, 54.1, 66.6], [0.000891, 53.2, 7.3], [-0.00133, 56.1, 15.8], [-0.00045, 43.6, 19.3], [0.004365, 68.8, 94.2], [-0.00355, 71.1, 76.1], [0, 71.3, 64.8], [-0.00415, 65.7, 59.4], [0.003881, 72.3, 47], [-0.00727, 54.7, 68.4], [0.001848, 63.3, 81.3], [0.002518, 69.7, 93.8], [-0.00252, 65.2, 84], [-0.0025, 55.8, 67.5], [0.004365, 68.2, 94.5], [0.008311, 76.4, 85.4], [0.000208, 35.4, 70.5], [-0.00021, 45, 92.7], [-0.00189, 32, 75.5], [0.000219, 32.3, 38.3], [0.001522, 33.4, 61.7], [0, 37.6, 37.1], [0.0016, 47.6, 32.5], [0.000595, 59.1, 7.1], [0, 63.1, 5], [0.00212, 55.3, 26.5], [0.002495, 64.7, 27.4], [0.00197, 79.4, 24.2], [0.006561, 75.2, 38.9], [0.011262, 82.3, 53.3], [0.019798, 88.7, 25.6], [-0.0198, 78.9, 45.2], [0.000602, 22.6, 74.7], [-0.00082, 29, 93.5], [0.002707, 43.8, 95], [0.000497, 27.7, 45.4], [-0.00131, 19.3, 28.7], [0.000152, 19.2, 38.3], [-0.00015, 23.5, 64.5], [0.001311, 31.7, 41.9], [0.004164, 54.8, 28.4], [-0.00185, 57.8, 32], [0.004771, 71.1, 69.3], [0.010728, 87.7, 92.3], [-0.0063, 81.6, 71.6], [0.079499, 84.6, 52.1], [-0.07294, 78.2, 52.1], [-0.00656, 80.6, 64.8], [0, 70.7, 67.2], [0.00541, 76.1, 75.5], [0.003548, 81.6, 93.2], [-0.00355, 74.9, 84.8], [-0.00016, 68.6, 60], [0.000164, 70, 17.2], [0.005518, 78.8, 16.3], [-0.00466, 64, 27.7], [0.000619, 66.5, 46], [-0.00552, 71.6, 77.9], [-0.00027, 72, 50.7], [0.002583, 46, 87.2], [0.007159, 69.6, 75.6], [0.005518, 77.1, 84.9], [-0.00729, 79.4, 92.4], [0.00197, 75.2, 75], [-0.00011, 74.7, 19.3], [0.011137, 90.9, 22.6], [0.006561, 77.8, 41.2], [-0.00186, 14.3, 38.6], [0.000497, 28, 61.2], [0.001303, 38.9, 55.7], [0.002049, 52.4, 41.8], [-0.00205, 41.9, 51.3], [-0.00055, 46.7, 70.2], [0.004365, 71.8, 94.5], [-0.00477, 65.8, 67.8], [0.00541, 75.5, 84.2], [-0.00541, 74.5, 72.2], [0.00541, 77.8, 85.7], [-0.00568, 72, 54.5], [0.001221, 46.9, 3.5], [0.003635, 58.2, 14.9], [0.000446, 45.2, 34], [0.004771, 68.4, 37.4], [0, 71.3, 55.1], [0, 67.6, 61.8], [0.022626, 94.2, 84.6], [-0.0026, 45.8, 69.8], [0.002565, 55.7, 74.9], [0.007159, 68, 76.1], [-0.00027, 71.1, 38.3], [0.019798, 91.3, 70.7], [0, 92.6, 92.4], [-0.01073, 74.6, 74.6], [0.005318, 87.2, 92.6], [-0.0063, 79.8, 70.5], [0.017925, 98.1, 78], [0.000668, 34.4, 91.2], [0, 36.2, 92.9], [0.000885, 38.2, 75.9], [-0.00094, 21.7, 47], [0.000939, 30.2, 31.3], [-0.00088, 37.9, 6.2], [-0.00016, 27.4, 36.7], [-0.00048, 39.8, 87.2], [0.005303, 63.2, 95.9], [0.001901, 61.8, 70.5], [0.000652, 22, 89.1], [0.003699, 33.6, 80.3], [0.005125, 60.3, 58.1], [0, 63.1, 38.5], [0, 65, 60.2], [0, 60.9, 39.1], [0.004152, 67.7, 27.1], [0.003441, 77, 9.7], [-0.00344, 74.6, 28], [0, 70.9, 49.7], [0.012118, 90.9, 23.8], [0.000366, 25.2, 93.3], [-0.00212, 58, 94.4], [-0.00225, 42.4, 93.5], [0.003635, 52, 85.7], [0.001332, 58.7, 94.5], [-0.00133, 53.9, 84.6], [-0.00388, 58.9, 45.1], [0.006956, 67, 48.2], [-0.007, 51.9, 67.8], [-0.00416, 39.1, 29.8], [-0.00253, 19.8, 44], [-0.00044, 45, 30.4], [-0.001, 45.9, 77.4], [0.002565, 53.1, 66.8], [-0.00416, 41.5, 35.2], [-0.00055, 46.9, 66], [0.000595, 64.1, 96], [0.001901, 61.2, 68.3], [-0.00252, 62.9, 9.4], [0, 73.4, 7.1], [0.025256, 94.3, 13.3], [-0.01084, 72.3, 16.4], [0.010991, 75.2, 56.3], [0.010999, 89.2, 87.5], [0.000164, 72.5, 80.4], [0.000107, 72, 69.6], [-0.00027, 73, 38.6], [0.002273, 56.7, 83.3], [-0.00035, 23.4, 77.6], [0.000353, 29.8, 87.5], [0.002707, 44, 95], [-0.00034, 34.3, 84.5], [-0.00122, 39.4, 59.7], [0.001219, 47.9, 77.1], [0.001001, 42.4, 58.1], [-0.00055, 43.9, 31.4], [0.000555, 45.1, 55.7], [0.003857, 58.6, 75.6], [0.002518, 67.5, 96.5], [-0.00059, 55.8, 73.7], [-0.00201, 49.5, 56.3], [-0.00078, 54, 17], [0.004152, 72, 66.5], [0.003441, 78.9, 91.4], [-0.00355, 70.4, 79.8], [-0.00027, 69.2, 61.7], [0.000271, 68.3, 36.2], [0.003441, 78.8, 11.2], [0, 82.8, 15.4], [-0.00098, 75.9, 33.5], [0, 75.2, 67.5], [0.012118, 90.5, 87], [0, 42.1, 10.4], [0.001808, 48.4, 31.4], [0.007266, 69.4, 26.6], [0.003548, 78.6, 6.2], [0.000989, 80.3, 28.1], [-0.00443, 74.8, 28.9], [0.001417, 10.9, 38.6], [0, 9.2, 46.3], [0.000497, 32.9, 49.4], [-0.0005, 8.6, 48.1], [0.000277, 28.1, 32.2], [-0.00232, 12.5, 23.3], [0.000991, 30.2, 3.5], [0.000818, 30.7, 18.7], [-0.00021, 43.8, 92.9], [0.000479, 39.6, 54.9], [0.00222, 48.8, 46], [-3.93E-05, 53.7, 29.9], [-0.00166, 35.1, 50.7], [-0.00116, 17.8, 45.1], [0.002461, 33.6, 44], [-0.00014, 46.2, 4], [0.001382, 54.5, 19.6], [3.93E-05, 54.5, 50.9], [0.001251, 73.6, 92.4], [0.000989, 75.2, 68.1], [-0.01036, 75.6, 94.4], [-0.00252, 66.5, 80.3], [0.005419, 69.7, 66.3], [0, 74.4, 74.4], [0, 70.9, 66.3], [0, 72.5, 72.5], [-0.00027, 67.6, 55.2], [0.016548, 90, 87.2], [0.067381, 86.9, 60.2], [-0.00656, 79.2, 71.6], [-0.0047, 73.7, 52.1], [0.005681, 78.1, 19.1], [0.006561, 78.2, 46.3], [-0.00656, 77.2, 69.3], [-0.0047, 72, 58.7], [-0.0045, 66, 67.4], [-0.00246, 53, 51.3], [0.007227, 66.9, 66.2], [0.003441, 79.8, 90.5], [-0.00016, 72, 54.5], [0.019798, 86.4, 29.5], [0.009515, 97.7, 25.6], [0.016818, 89.1, 76.8], [-0.01212, 80.7, 68.6], [0.013629, 85.6, 96.9], [-0.00729, 81, 96], [-0.00355, 71, 82.1], [-0.00011, 70.4, 24.5], [0.005518, 76.7, 14.9], [-0.00027, 71, 45.2], [0.005681, 79, 85.4], [-0.00568, 72.5, 53.6], [0, 72.5, 41.9], [0, 29.7, 66], [0, 31.7, 72.6], [0, 29.7, 64.2], [0.00222, 44.1, 44.9], [-0.0027, 37.8, 17.8], [0.000336, 44.3, 5], [-0.00034, 37.6, 16.4], [0.000336, 44.4, 12.1], [-0.00024, 29.9, 88.2], [0.000589, 23.2, 70.2], [0.001522, 33.9, 42.8], [-0.0006, 5.7, 45.7], [0.000602, 23, 37.1], [-0.00024, 25.9, 4.3], [-0.0042, 40.7, 68], [0.001154, 43.4, 87], [0.000891, 55.2, 93.5], [-0.00225, 42, 88.7], [-0.00138, 31.2, 74.7], [-0.00094, 16.8, 49.4], [0.002526, 41.4, 72.9], [0.000891, 55.9, 94.5], [-0.00133, 53, 75.3], [0.010668, 77.5, 95.7], [0.000989, 76.1, 68.1], [0.019798, 87.9, 30.8], [0.003771, 72.5, 96.5], [0.002253, 54.5, 93.2], [-0.00225, 43.3, 92.7], [-0.00271, 27.4, 91.7], [0.001051, 14.2, 54], [0.003464, 43.5, 67.1], [0.000555, 47.1, 57.5], [0.00201, 56.5, 72.5], [0.00187, 73.4, 95.6], [0.008311, 75.8, 77.9], [-0.00541, 67.2, 69.9], [-0.00027, 73.9, 54.6], [-0.01126, 71.8, 57], [0.004701, 81.4, 34.4], [0.000232, 9.2, 40.6], [0.000277, 26.3, 26.8], [-0.00075, 4.1, 51.5], [-0.00062, 8.5, 83.6], [0.002706, 46.6, 93.9], [0.003881, 68.7, 58.8], [0, 74.6, 47.5], [0, 70.7, 47.5], [-0.00149, 30.1, 90.6], [0.002097, 36.6, 65.4], [6.53E-05, 34.2, 30.3], [0.002044, 54.9, 4.1], [0, 54, 10.7], [0.004771, 68.9, 73.8], [0.00541, 82, 82.7], [-0.00098, 75.8, 64.2], [0, 79.5, 74.9], [-0.00552, 74.9, 75.9], [0.016944, 92.2, 85.5], [-0.00634, 70, 92.4], [0, 73.7, 96.2], [-0.00377, 61.8, 96.2], [0.00212, 53.7, 69], [0.002495, 59.7, 66.9], [0, 74, 83.4], [0.005518, 77.8, 86.3], [0.003548, 80.9, 93.6], [0, 79.1, 51.9], [-0.01126, 73.6, 55.5], [-0.00733, 75, 93.3], [-0.00344, 74.4, 71.3], [-0.00477, 66.3, 71], [0.004664, 68.1, 75.9], [0.000107, 69.9, 65.6], [-0.00027, 74.3, 55.7], [0, 66.7, 93], [0.002901, 68.1, 72.3], [0.00443, 75.2, 74.9], [0, 72.8, 92.3], [0.006342, 77.6, 95.6], [-0.00634, 70, 88.1], [-0.00016, 68.6, 57.8], [-0.00062, 66.4, 35.5], [0.000271, 73, 68.7], [-0.00388, 65.7, 53.7], [0.007592, 81.5, 11.3], [-0.01655, 74.3, 29.3], [-0.00388, 61.3, 53.9], [0.003881, 69, 49.9], [0.000271, 74.3, 64.5], [0.012118, 88.9, 86.1], [0.002565, 50.1, 66], [0.004326, 71.7, 7.1], [-0.0047, 71.3, 41.5], [0.004701, 79.3, 64.1], [-0.00843, 92.7, 76.2], [0.001253, 59.4, 80.3], [0, 56, 33.7], [0.000595, 65.4, 5.3], [0.001253, 61.5, 14.2], [0.001267, 65.2, 37.9], [0.003548, 77.4, 93.2], [-0.00355, 72.7, 85.4], [-0.00016, 70.7, 54.2], [0.026359, 91.4, 72.9], [-0.00729, 76.8, 6.1], [-0.00022, 29.6, 70.2], [0.001522, 36.5, 61.8], [0.011496, 67, 37.4], [-0.00477, 64.4, 26.8], [-0.00027, 74.5, 39.8], [0.019798, 90.3, 68.6], [0.000367, 14.6, 69], [0.001327, 28.8, 63.3], [0.001043, 37, 78.9], [0.000336, 42.2, 95.3], [-0.00189, 27.2, 85.1], [0.00222, 46.6, 56.6], [0.001328, 26.1, 93.8], [0.002707, 42.3, 92.3], [0.00011, 44.9, 38.5], [0.0045, 67.6, 38.8], [0, 62, 45.7], [0.007592, 75.3, 91.2], [0, 18.8, 55.4], [-0.00039, 13.4, 68.1], [-0.00014, 46.5, 89.7], [0, 42.4, 89.1], [0.002253, 53.8, 95], [0.005318, 85.7, 89.4], [-0.01212, 75.1, 66.3], [0.005681, 80.7, 14.6], [-0.00098, 76.1, 31.3], [-0.00027, 67.3, 56.9], [0.003712, 82.5, 87.8], [-0.01212, 80.2, 70.4], [-0.00541, 70.1, 74.4], [0.00541, 79.9, 85.8], [0, 75.2, 75.9], [-0.00098, 79.7, 72.9], [-0.00443, 72.1, 72.6], [0.00443, 76.6, 74.7], [-0.00443, 70.6, 73.5], [0.00443, 78.7, 65], [-0.0047, 72.7, 54.5], [0.003712, 82.8, 11.5], [-0.01655, 74.7, 29.8], [-0.00027, 73.3, 41.9], [0, 74.2, 57.6], [0.016548, 84.6, 86.1], [-0.01665, 74.3, 75], [0.004537, 80.3, 71.4], [-0.00116, 27.8, 47.3], [0, 39.1, 93.5], [0.003473, 53.5, 96], [-0.00122, 40.4, 91.1], [-0.00122, 34.4, 51], [0.002109, 52.5, 5.3], [0.00212, 52.7, 25.7], [0, 55.6, 36.7], [0.006996, 67.1, 43.9], [-0.00388, 62.7, 45.4], [0.001251, 74.1, 90.6], [0, 73.9, 63.3], [0, 70.7, 67.2], [-0.00027, 68.1, 44.3], [0, 65, 60.9], [0.004045, 73.5, 83.6], [0.00443, 81.6, 68.9], [1.33E-05, 22.2, 78], [0.010728, 85.8, 87.8], [-0.01665, 72.3, 78.6], [-0.00011, 72.6, 13.7], [0.00201, 51.7, 65.3], [-0.00256, 45.9, 68.8], [0.001848, 59.7, 78.6], [0.005419, 68.6, 73.2], [-0.00656, 78, 36.4], [0.000981, 80.4, 87.2], [-0.00541, 74.7, 71.3], [-0.00308, 87.4, 92.6], [-0.00532, 76.6, 85.8], [0.005318, 86.8, 93.2], [-0.01084, 72.8, 76.5], [0.004701, 81.8, 29.6], [0.006561, 81.9, 47.8], [-0.00089, 46.9, 79.8], [-0.00122, 39.3, 45.2], [0.00222, 46.3, 59.6], [0.001272, 55.6, 87.3], [0.002715, 64.9, 96.2], [-0.00125, 64, 91.4], [0.001253, 65.4, 78.9], [-0.00125, 63, 87.9], [-0.00059, 54.7, 73.4], [-0.00074, 54, 16.6], [0.001332, 60.6, 6.5], [0.000619, 64.9, 59.6], [0.001251, 72.3, 89.1], [0.011137, 85.4, 84.6], [0.001332, 66.6, 93.6], [-0.00252, 64.2, 86.3], [0.005312, 74.1, 77.6], [-0.00531, 66, 77.1], [0.002518, 70.1, 93.3], [-0.00252, 64.5, 83.6], [-0.00185, 54.7, 65.5], [-3.93E-05, 51.5, 35.2], [0.000595, 60.9, 4.7], [-0.00059, 58.3, 30.5], [-0.00415, 63, 52], [0.004664, 67.1, 77.4], [0.005518, 83, 87.3], [-0.0198, 79.4, 60.3], [0.202542, 97, 44.5], [0.000367, 10.1, 36.7], [-0.00037, 7.9, 49.3], [0, 29.8, 48.1], [0.001043, 37.5, 80], [0, 34.4, 54.2], [-0.00246, 24.3, 58.2], [0.002461, 35.9, 58.1], [-0.00048, 33.5, 86], [-0.00088, 41.6, 95.3], [0, 36, 89.6], [0.00305, 55.7, 56.9], [-0.0027, 37.1, 13.7], [0.003473, 54.3, 4.1], [0.000738, 56, 37.3], [3.93E-05, 53.8, 59.7], [0.00187, 74.6, 90.2], [0, 75.9, 66.3], [0.012118, 87.2, 83.7], [-0.01212, 75.1, 71.9], [0.012118, 87.2, 19.6], [-0.00541, 75, 29.9], [0.000164, 70.3, 75.8], [0.005518, 82.1, 86], [-0.00541, 74.2, 69.9], [-0.00027, 74.4, 60.8], [0.0842, 88.8, 62.3], [0.002518, 70.1, 91.4], [0.013107, 91.5, 85.1], [-0.00729, 81.8, 95.7], [-0.00355, 74.7, 80.6], [0.000107, 74.9, 66], [0, 72.1, 72.2], [0.000164, 69.7, 20.8], [0.000107, 71.4, 27.2], [-0.00477, 64.5, 32.5], [-0.00477, 65.4, 70.2], [0.004664, 70.2, 86], [0.003548, 80.7, 92.9], [-0.00355, 73.9, 78.5], [0.026359, 89.4, 68.7], [0, 92.4, 53.6], [0.061574, 84.9, 39.8], [0.011696, 76.6, 25.9], [-0.00027, 74.3, 57.9], [0.003631, 53.2, 12.2], [0, 59.8, 53.7], [0.004152, 70.6, 71.3], [0, 69.8, 64.7], [0.011262, 76.7, 53.4], [-0.00388, 66, 46.4], [0.020699, 86.3, 81], [0.011137, 90, 81], [-0.01114, 79.2, 75.2], [-0.00308, 84.5, 95.6], [-0.01084, 71.5, 86.4], [0.010836, 86.6, 94.4], [-0.01363, 73.5, 91.7], [0.002794, 69.6, 77.7], [0.005518, 76.5, 76.2], [0.004204, 54.4, 57], [0.003075, 60.3, 50.1], [-0.00427, 40.3, 54.9], [0.001219, 44.7, 84.3], [0.000891, 58.2, 94.5], [0, 56, 89.6], [0, 51.2, 88.7], [0.000738, 52.8, 65.4], [0, 57.8, 69.5], [0.002495, 61.5, 63.9], [0, 65, 69.3], [-0.00377, 65.2, 89.7], [-0.00185, 54.3, 64.1], [0, 51.6, 65.1], [0.001272, 50.8, 21.8], [0, 55, 33.5], [-0.00575, 33, 30.8], [-0.0006, 7.5, 51], [-0.00191, 7.1, 20.6], [0.000531, 2.9, 43.7], [0.001656, 45, 89.1], [-0.00125, 66.3, 89.3], [0, 65.4, 67.2], [0.016818, 85, 13.1], [-0.02636, 78.4, 36.8], [0.005681, 78.8, 78.2], [-0.00098, 79.8, 71.1], [0.006561, 75.7, 43], [0, 68.7, 46.4], [0.005681, 78.3, 19.7], [-0.01665, 71.7, 24.8], [0.003441, 80.7, 11.8], [-0.00355, 68.2, 20.2], [-0.00415, 65, 60.6], [0, 72.7, 57.2], [-0.00011, 67.9, 79.1], [0.000107, 71.6, 74.9], [-0.00011, 67.2, 78.6], [-0.0042, 37.3, 27.7], [-0.00054, 37.8, 14.3], [0.000545, 37.4, 36.1], [-0.00152, 32, 65.9], [0.000219, 29.8, 45.1], [-0.00022, 31.7, 63.6], [0.001303, 39.8, 40.7], [0.000823, 41, 20], [-0.00067, 29.7, 23.5], [0.003187, 44.3, 69.5], [0.00506, 64.5, 68.7], [0.002518, 74.1, 99.3], [0.00755, 80.7, 59.7], [-0.03587, 81, 62.6], [0.009374, 93.8, 5.3], [0.010363, 95.3, 6.5], [-0.01793, 82.2, 28.1], [0.000388, 20.7, 41.9], [-0.0019, 23.5, 24.7], [0.000814, 12.2, 51.8], [0.000277, 31.1, 66.2], [0.002144, 48.1, 72.3], [0.010836, 84.4, 94.1], [0.0045, 69.2, 56], [0.001303, 38.1, 45.4], [-0.0013, 25.8, 49.4], [-0.00062, 66.3, 25.6], [0, 57.4, 90.8], [0.003115, 61.3, 43.3], [-0.00066, 17.1, 43.1], [0.000662, 10.3, 56.7], [0.000497, 27.6, 59.3], [0.003187, 44.1, 72.2], [-0.00303, 39.6, 92.1], [-0.00149, 32.8, 90.9], [0.003473, 55, 96.2], [-0.00347, 39.5, 89.3], [0.000479, 33.9, 41], [0.002565, 52.4, 33.4], [0.001848, 62.5, 13.3], [0.000648, 62.2, 31], [0.004152, 67.1, 66.2], [0.00541, 81.6, 84.2], [0.0265, 95.9, 64.8], [0.001552, 37.7, 78.8], [0, 39, 87.2], [0.00853, 70.5, 96.2], [-0.00062, 9.6, 14.5], [0.001809, 26.4, 18.8], [0.000668, 40.1, 6.8], [3.93E-05, 55.3, 39.2], [0.004152, 68, 68.6], [0.00541, 77.5, 84.2], [0.000107, 72, 65.7], [0.00622, 61.8, 14.8], [-0.00065, 58.5, 13.9], [0.002518, 68.2, 7.9], [-0.00011, 72.2, 24.5], [0.016655, 85.7, 15.5], [-0.02636, 75.3, 29.6], [-0.00027, 72.4, 49.6], [0.000366, 29.9, 88.7], [0.009959, 72.8, 87.8], [-0.01212, 76.3, 67.8], [0.00443, 82.2, 27.4], [-3.93E-05, 57, 64.2], [-0.00201, 45.6, 46.3], [0.005125, 59.7, 47.6], [-0.00062, 63.7, 63.5], [0.004664, 73.9, 76.4], [-0.00098, 75.6, 27.7], [0.026359, 85, 32.2], [0.000948, 23.1, 90.8], [0.002915, 36, 31.7], [0.002044, 57, 10.4], [0.003473, 57.4, 96.5], [-0.00027, 67.8, 55.2], [0.005681, 75.2, 17], [-0.00088, 39.3, 9.5], [0.006188, 60.3, 4.7], [0.002901, 67.2, 26], [0.030789, 86.1, 74.3], [-0.0198, 76.8, 57.8], [-0.00252, 65.7, 91.5], [-0.00133, 58.3, 86.1], [0.000738, 55.8, 67.2], [0.002495, 58.9, 70.4], [0.003881, 71.7, 45.4], [-0.00263, 68.7, 97.5], [-1.33E-05, 6.4, 54.2], [0.001656, 46.7, 93.5], [-0.00377, 62.9, 8.2], [0.001267, 60, 61.5], [-0.00062, 65.4, 67.4], [0.001251, 71.4, 8.5], [0.000755, 21.1, 58.5], [-0.00075, 6, 52.4], [0.008524, 60.2, 66.2], [0, 66.4, 70.4], [0.004771, 67.7, 74.3], [0, 72.2, 62.9], [0, 72.2, 68.4], [0.040304, 98, 73.8],
]
)
df2 = df.pivot_table("z", index="y", columns="x", aggfunc="sum").fillna(0)
array = df2.sort_index(ascending=False).values
The beauty of pivot_table is that this will sort your x/y values.
Be carefull though, your y value would be ascending (top to bottom), which is the opposite of what you would want in a picture. You have to re-sort the index in reverse mode (hence the sort_index command).
There is also an assumption here : that your x's and y's values are linear OR that you are not trying to plot anything linear. In fact, you data sample are not (for instance, your first discrete y values are 3.5, 4, 4.1) ; you simply cannot use that kind of coordinates for anything in a grid format.
Given the fact that your datas' coordinates are all float with one decimal (at most), you could complete your datas this way :
all_xs = all_ys = range(0, 1001) # get all discrete xs and ys values (on an integer basis)
missing_xs = list(set(all_xs) - set(df['x']*10)) #find all missing xs
missing_ys = list(set(all_ys) - set(df['y']*10)) #find all missing ys
dummy_df = pd.DataFrame(0, index=missing_ys, columns=missing_xs) #construct a dummy dataframe with those xs and ys (filled with zeros)
df2.index *= 10 #convert your indexes and columns of the previous df2 to integers
df2.columns *= 10
df3 = df2.append(dummy_df).fillna(0) #append the dataframes, which will create the missing columns and index and fill all empty values with zeros
df3.sort_index(inplace=True, ascending=False) #resort the index (ie ys values)
df3.sort_index(axis=1, inplace=True, ascending=True) #resort the columns (ie xs values)
print(df3.shape) #make sure the shape is a square array
array = df3.values #extract the numpy array
you can convert the column data into a numpy array and reshape it to your required dimensions:
In [57]: df = pd.DataFrame({"A": [1, 2,3,4,5,6], "B": [3.0, 4.5,9,8,36,6]})
In [58]: df
Out[58]:
A B
0 1 3.0
1 2 4.5
2 3 9.0
3 4 8.0
4 5 36.0
5 6 6.0
In [59]: res= np.array(df['B'].values.tolist())
In [60]: res
Out[60]: array([ 3. , 4.5, 9. , 8. , 36. , 6. ])
In [61]: res.reshape(3,2)
Out[61]:
array([[ 3. , 4.5],
[ 9. , 8. ],
[36. , 6. ]])

Python : Finding the closest RGB values from a list of RGB colors

I am kind of stuck in here, and couldn't find the best performance-effective algorithms to find the 5 closest values to the target value from a list of RGB lists.
The color list:
[[212, 211, 211], [159, 148, 138], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179]]
Target color: [202, 200, 198]
the expected valaue:
[[#,#,#], [#,#,#], [#,#,#], [#,#,#], [#,#,#]]
I am using this set of color lists to find the images with similar colors. There may be duplicated values, and omitting the exact value should be considered as these were extracted from a set of images that can be the exact same photos.
Using euclidean distance:
import math
def distance(a, b):
return math.hypot(*(v2 - v1 for v1, v2 in zip(a, b)))
colors = [[212, 211, 211], [159, 148, 138], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179], [216, 213, 211], [185, 176, 167], [186, 183, 179], [177, 175, 174], [184, 172, 157], [229, 225, 221], [53, 48, 44], [127, 114, 103], [230, 229, 227], [218, 215, 210], [209, 203, 192], [96, 82, 68], [121, 100, 86], [206, 204, 199], [151, 149, 132], [232, 233, 233], [50, 46, 45], [188, 173, 163], [185, 164, 140], [136, 126, 115], [202, 200, 198], [124, 111, 97], [122, 101, 85], [212, 211, 211], [159, 148, 138], [172, 171, 164], [114, 106, 97], [96, 82, 68], [92, 85, 74], [200, 196, 194], [191, 189, 189], [186, 182, 179]]
target = [202, 200, 198]
closest = sorted(colors, key=lambda x: distance(target, x))
print(closest[:5])
Without detail definition about what's "closest" RGB, I think this helper may offer you some idea?
import math
def distance(c1, c2):
r1,g1,b1 = c1
r2,g2,b2 = c2
return math.sqrt((r1 - r2)**2 + (g1 - g2) ** 2 + (b1 - b2) **2)
Then you could pass the color_list to check and match.

I'm trying to remove nested list which contains bounding box values. I'm using easyocr library to extract the text and bounding box values

bounds = reader.readtext(np.array(images[0]), min_size=0, slope_ths=0.2, ycenter_ths=0.7, height_ths=0.6, width_ths=0.8,decoder='beamsearch', beamWidth=10)
print(bounds)
[([[1002, 126], [1210, 126], [1210, 222], [1002, 222]],
'uz5',
0.048652395606040955),
([[177, 179], [349, 179], [349, 241], [177, 241]],
'OIZI',
0.7936368584632874),
([[180, 236], [422, 236], [422, 268], [180, 268]],
'Oki Electric Industry Co',
0.4165174067020416)]
print(bounds[0][0])
[[1002, 126], [1210, 126], [1210, 222], [1002, 222]]
How to remove the nested list and make a flat list for all the bounding box values in 'bounds' variable?
This approach makes a list which reduces the nested list into a flattened list found in the first element -> bound[0] of each entry of bounds, the other elements are unpacked in the tuple, after the flattened list.
bounds = [([[1002, 126], [1210, 126], [1210, 222], [1002, 222]], 'uz5', 0.048652395606040955), ([[177, 179], [349, 179], [349, 241], [177, 241]], 'OIZI', 0.7936368584632874), ([[180, 236], [422, 236], [422, 268], [180, 268]], 'Oki Electric Industry Co', 0.4165174067020416)]
out = [(sum(bound[0], []), *bound[1:]) for bound in bounds]
print(out)
Output:
[([1002, 126, 1210, 126, 1210, 222, 1002, 222], 'uz5', 0.048652395606040955),
([177, 179, 349, 179, 349, 241, 177, 241], 'OIZI', 0.7936368584632874),
([180, 236, 422, 236, 422, 268, 180, 268], 'Oki Electric Industry Co', 0.4165174067020416)]
Alternatively if you only intented to keep the first flattened list you could do this:
bounds = [([[1002, 126], [1210, 126], [1210, 222], [1002, 222]], 'uz5', 0.048652395606040955), ([[177, 179], [349, 179], [349, 241], [177, 241]], 'OIZI', 0.7936368584632874), ([[180, 236], [422, 236], [422, 268], [180, 268]], 'Oki Electric Industry Co', 0.4165174067020416)]
out = [sum(bound[0], []) for bound in bounds]
print(out)
Output:
[[1002, 126, 1210, 126, 1210, 222, 1002, 222],
[177, 179, 349, 179, 349, 241, 177, 241],
[180, 236, 422, 236, 422, 268, 180, 268]]

Greatly increase performance of 1 section of code

I have got some code here to solve a challenge problem(If you need to see it just ask):
from collections import deque
def breadthfirstsearch(graph, start, goal):
if start == goal:
return [start]
visited = {start}
queue = deque([(start, [])])
while queue:
current, path = queue.popleft()
visited.add(current)
for neighbor in graph[current]:
if neighbor == goal:
return path + [current, neighbor]
if neighbor in visited:
continue
queue.append((neighbor, path + [current]))
visited.add(neighbor)
return None
'''
ONLY FOR CHALLENGE PROBLEM IRRELEVANT NOW
x = 0 #int(input())
final =[]
for i in range(x):
a, b, c, d = list(map(int, input().split()))
lne = []
for j in range(b):
t = list(map(int, input().split()))
lne.append(t)
r = [c, d, lne]
final.append(r)
'''
roadCounts = 0
final = [[100, 1000, [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22], [22, 23], [23, 24], [24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31], [31, 32], [32, 33], [33, 34], [34, 35], [35, 36], [36, 37], [37, 38], [38, 39], [39, 40], [40, 41], [41, 42], [42, 43], [43, 44], [44, 45], [45, 46], [46, 47], [47, 48], [48, 49], [49, 50], [50, 51], [51, 52], [52, 53], [53, 54], [54, 55], [55, 56], [56, 57], [57, 58], [58, 59], [59, 60], [60, 61], [61, 62], [62, 63], [63, 64], [64, 65], [65, 66], [66, 67], [67, 68], [68, 69], [69, 70], [70, 71], [71, 72], [72, 73], [73, 74], [74, 75], [75, 76], [76, 77], [77, 78], [78, 79], [79, 80], [80, 81], [81, 82], [82, 83], [83, 84], [84, 85], [85, 86], [86, 87], [87, 88], [88, 89], [89, 90], [90, 91], [91, 92], [92, 93], [93, 94], [94, 95], [95, 96], [96, 97], [97, 98], [98, 99], [99, 100], [100, 101], [101, 102], [102, 103], [103, 104], [104, 105], [105, 106], [106, 107], [107, 108], [108, 109], [109, 110], [110, 111], [111, 112], [112, 113], [113, 114], [114, 115], [115, 116], [116, 117], [117, 118], [118, 119], [119, 120], [120, 121], [121, 122], [122, 123], [123, 124], [124, 125], [125, 126], [126, 127], [127, 128], [128, 129], [129, 130], [130, 131], [131, 132], [132, 133], [133, 134], [134, 135], [135, 136], [136, 137], [137, 138], [138, 139], [139, 140], [140, 141], [141, 142], [142, 143], [143, 144], [144, 145], [145, 146], [146, 147], [147, 148], [148, 149], [149, 150], [150, 151], [151, 152], [152, 153], [153, 154], [154, 155], [155, 156], [156, 157], [157, 158], [158, 159], [159, 160], [160, 161], [161, 162], [162, 163], [163, 164], [164, 165], [165, 166], [166, 167], [167, 168], [168, 169], [169, 170], [170, 171], [171, 172], [172, 173], [173, 174], [174, 175], [175, 176], [176, 177], [177, 178], [178, 179], [179, 180], [180, 181], [181, 182], [182, 183], [183, 184], [184, 185], [185, 186], [186, 187], [187, 188], [188, 189], [189, 190], [190, 191], [191, 192], [192, 193], [193, 194], [194, 195], [195, 196], [196, 197], [197, 198], [198, 199], [199, 200], [200, 201], [201, 202], [202, 203], [203, 204], [204, 205], [205, 206], [206, 207], [207, 208], [208, 209], [209, 210], [210, 211], [211, 212], [212, 213], [213, 214], [214, 215], [215, 216], [216, 217], [217, 218], [218, 219], [219, 220], [220, 221], [221, 222], [222, 223], [223, 224], [224, 225], [225, 226], [226, 227], [227, 228], [228, 229], [229, 230], [230, 231], [231, 232], [232, 233], [233, 234], [234, 235], [235, 236], [236, 237], [237, 238], [238, 239], [239, 240], [240, 241], [241, 242], [242, 243], [243, 244], [244, 245], [245, 246], [246, 247], [247, 248], [248, 249], [249, 250], [250, 251], [251, 252], [252, 253], [253, 254], [254, 255], [255, 256], [256, 257], [257, 258], [258, 259], [259, 260], [260, 261], [261, 262], [262, 263], [263, 264], [264, 265], [265, 266], [266, 267], [267, 268], [268, 269], [269, 270], [270, 271], [271, 272], [272, 273], [273, 274], [274, 275], [275, 276], [276, 277], [277, 278], [278, 279], [279, 280], [280, 281], [281, 282], [282, 283], [283, 284], [284, 285], [285, 286], [286, 287], [287, 288], [288, 289], [289, 290], [290, 291], [291, 292], [292, 293], [293, 294], [294, 295], [295, 296], [296, 297], [297, 298], [298, 299], [299, 300], [300, 301], [301, 302], [302, 303], [303, 304], [304, 305], [305, 306], [306, 307], [307, 308], [308, 309], [309, 310], [310, 311], [311, 312], [312, 313], [313, 314], [314, 315], [315, 316], [316, 317], [317, 318], [318, 319], [319, 320], [320, 321], [321, 322], [322, 323], [323, 324], [324, 325], [325, 326], [326, 327], [327, 328], [328, 329], [329, 330], [330, 331], [331, 332], [332, 333], [333, 334], [334, 335], [335, 336], [336, 337], [337, 338], [338, 339], [339, 340], [340, 341], [341, 342], [342, 343], [343, 344], [344, 345], [345, 346], [346, 347], [347, 348], [348, 349], [349, 350], [350, 351], [351, 352], [352, 353], [353, 354], [354, 355], [355, 356], [356, 357], [357, 358], [358, 359], [359, 360], [360, 361], [361, 362], [362, 363], [363, 364], [364, 365], [365, 366], [366, 367], [367, 368], [368, 369], [369, 370], [370, 371], [371, 372], [372, 373], [373, 374], [374, 375], [375, 376], [376, 377], [377, 378], [378, 379], [379, 380], [380, 381], [381, 382], [382, 383], [383, 384], [384, 385], [385, 386], [386, 387], [387, 388], [388, 389], [389, 390], [390, 391], [391, 392], [392, 393], [393, 394], [394, 395], [395, 396], [396, 397], [397, 398], [398, 399], [399, 400], [400, 401], [401, 402], [402, 403], [403, 404], [404, 405], [405, 406], [406, 407], [407, 408], [408, 409], [409, 410], [410, 411], [411, 412], [412, 413], [413, 414], [414, 415], [415, 416], [416, 417], [417, 418], [418, 419], [419, 420], [420, 421], [421, 422], [422, 423], [423, 424], [424, 425], [425, 426], [426, 427], [427, 428], [428, 429], [429, 430], [430, 431], [431, 432], [432, 433], [433, 434], [434, 435], [435, 436], [436, 437], [437, 438], [438, 439], [439, 440], [440, 441], [441, 442], [442, 443], [443, 444], [444, 445], [445, 446], [446, 447], [447, 448], [448, 449], [449, 450], [450, 451], [451, 452], [452, 453], [453, 454], [454, 455], [455, 456], [456, 457], [457, 458], [458, 459], [459, 460], [460, 461], [461, 462], [462, 463], [463, 464], [464, 465], [465, 466], [466, 467], [467, 468], [468, 469], [469, 470], [470, 471], [471, 472], [472, 473], [473, 474], [474, 475], [475, 476], [476, 477], [477, 478], [478, 479], [479, 480], [480, 481], [481, 482], [482, 483], [483, 484], [484, 485], [485, 486], [486, 487], [487, 488], [488, 489], [489, 490], [490, 491], [491, 492], [492, 493], [493, 494], [494, 495], [495, 496], [496, 497], [497, 498], [498, 499], [499, 500], [500, 501], [501, 502], [502, 503], [503, 504], [504, 505], [505, 506], [506, 507], [507, 508], [508, 509], [509, 510], [510, 511], [511, 512], [512, 513], [513, 514], [514, 515], [515, 516], [516, 517], [517, 518], [518, 519], [519, 520], [520, 521], [521, 522], [522, 523], [523, 524], [524, 525], [525, 526], [526, 527], [527, 528], [528, 529], [529, 530], [530, 531], [531, 532], [532, 533], [533, 534], [534, 535], [535, 536], [536, 537], [537, 538], [538, 539], [539, 540], [540, 541], [541, 542], [542, 543], [543, 544], [544, 545], [545, 546], [546, 547], [547, 548], [548, 549], [549, 550], [550, 551], [551, 552], [552, 553], [553, 554], [554, 555], [555, 556], [556, 557], [557, 558], [558, 559], [559, 560], [560, 561], [561, 562], [562, 563], [563, 564], [564, 565], [565, 566], [566, 567], [567, 568], [568, 569], [569, 570], [570, 571], [571, 572], [572, 573], [573, 574], [574, 575], [575, 576], [576, 577], [577, 578], [578, 579], [579, 580], [580, 581], [581, 582], [582, 583], [583, 584], [584, 585], [585, 586], [586, 587], [587, 588], [588, 589], [589, 590], [590, 591], [591, 592], [592, 593], [593, 594], [594, 595], [595, 596], [596, 597], [597, 598], [598, 599], [599, 600], [600, 601], [601, 602], [602, 603], [603, 604], [604, 605], [605, 606], [606, 607], [607, 608], [608, 609], [609, 610], [610, 611], [611, 612], [612, 613], [613, 614], [614, 615], [615, 616], [616, 617], [617, 618], [618, 619], [619, 620], [620, 621], [621, 622], [622, 623], [623, 624], [624, 625], [625, 626], [626, 627], [627, 628], [628, 629], [629, 630], [630, 631], [631, 632], [632, 633], [633, 634], [634, 635], [635, 636], [636, 637], [637, 638], [638, 639], [639, 640], [640, 641], [641, 642], [642, 643], [643, 644], [644, 645], [645, 646], [646, 647], [647, 648], [648, 649], [649, 650], [650, 651], [651, 652], [652, 653], [653, 654], [654, 655], [655, 656], [656, 657], [657, 658], [658, 659], [659, 660], [660, 661], [661, 662], [662, 663], [663, 664], [664, 665], [665, 666], [666, 667], [667, 668], [668, 669], [669, 670], [670, 671], [671, 672], [672, 673], [673, 674], [674, 675], [675, 676], [676, 677], [677, 678], [678, 679], [679, 680], [680, 681], [681, 682], [682, 683], [683, 684], [684, 685], [685, 686], [686, 687], [687, 688], [688, 689], [689, 690], [690, 691], [691, 692], [692, 693], [693, 694], [694, 695], [695, 696], [696, 697], [697, 698], [698, 699], [699, 700], [700, 701], [701, 702], [702, 703], [703, 704], [704, 705], [705, 706], [706, 707], [707, 708], [708, 709], [709, 710], [710, 711], [711, 712], [712, 713], [713, 714], [714, 715], [715, 716], [716, 717], [717, 718], [718, 719], [719, 720], [720, 721], [721, 722], [722, 723], [723, 724], [724, 725], [725, 726], [726, 727], [727, 728], [728, 729], [729, 730], [730, 731], [731, 732], [732, 733], [733, 734], [734, 735], [735, 736], [736, 737], [737, 738], [738, 739], [739, 740], [740, 741], [741, 742], [742, 743], [743, 744], [744, 745], [745, 746], [746, 747], [747, 748], [748, 749], [749, 750], [750, 751], [751, 752], [752, 753], [753, 754], [754, 755], [755, 756], [756, 757], [757, 758], [758, 759], [759, 760], [760, 761], [761, 762], [762, 763], [763, 764], [764, 765], [765, 766], [766, 767], [767, 768], [768, 769], [769, 770], [770, 771], [771, 772], [772, 773], [773, 774], [774, 775], [775, 776], [776, 777], [777, 778], [778, 779], [779, 780], [780, 781], [781, 782], [782, 783], [783, 784], [784, 785], [785, 786], [786, 787], [787, 788], [788, 789], [789, 790], [790, 791], [791, 792], [792, 793], [793, 794], [794, 795], [795, 796], [796, 797], [797, 798], [798, 799], [799, 800], [800, 801], [801, 802], [802, 803], [803, 804], [804, 805], [805, 806], [806, 807], [807, 808], [808, 809], [809, 810], [810, 811], [811, 812], [812, 813], [813, 814], [814, 815], [815, 816], [816, 817], [817, 818], [818, 819], [819, 820], [820, 821], [821, 822], [822, 823], [823, 824], [824, 825], [825, 826], [826, 827], [827, 828], [828, 829], [829, 830], [830, 831], [831, 832], [832, 833], [833, 834], [834, 835], [835, 836], [836, 837], [837, 838], [838, 839], [839, 840], [840, 841], [841, 842], [842, 843], [843, 844], [844, 845], [845, 846], [846, 847], [847, 848], [848, 849], [849, 850], [850, 851], [851, 852], [852, 853], [853, 854], [854, 855], [855, 856], [856, 857], [857, 858], [858, 859], [859, 860], [860, 861], [861, 862], [862, 863], [863, 864], [864, 865], [865, 866], [866, 867], [867, 868], [868, 869], [869, 870], [870, 871], [871, 872], [872, 873], [873, 874], [874, 875], [875, 876], [876, 877], [877, 878], [878, 879], [879, 880], [880, 881], [881, 882], [882, 883], [883, 884], [884, 885], [885, 886], [886, 887], [887, 888], [888, 889], [889, 890], [890, 891], [891, 892], [892, 893], [893, 894], [894, 895], [895, 896], [896, 897], [897, 898], [898, 899], [899, 900], [900, 901], [901, 902], [902, 903], [903, 904], [904, 905], [905, 906], [906, 907], [907, 908], [908, 909], [909, 910], [910, 911], [911, 912], [912, 913], [913, 914], [914, 915], [915, 916], [916, 917], [917, 918], [918, 919], [919, 920], [920, 921], [921, 922], [922, 923], [923, 924], [924, 925], [925, 926], [926, 927], [927, 928], [928, 929], [929, 930], [930, 931], [931, 932], [932, 933], [933, 934], [934, 935], [935, 936], [936, 937], [937, 938], [938, 939], [939, 940], [940, 941], [941, 942], [942, 943], [943, 944], [944, 945], [945, 946], [946, 947], [947, 948], [948, 949], [949, 950], [950, 951], [951, 952], [952, 953], [953, 954], [954, 955], [955, 956], [956, 957], [957, 958], [958, 959], [959, 960], [960, 961], [961, 962], [962, 963], [963, 964], [964, 965], [965, 966], [966, 967], [967, 968], [968, 969], [969, 970], [970, 971], [971, 972], [972, 973], [973, 974], [974, 975], [975, 976], [976, 977], [977, 978], [978, 979], [979, 980], [980, 981], [981, 982], [982, 983], [983, 984], [984, 985], [985, 986], [986, 987], [987, 988], [988, 989], [989, 990], [990, 991], [991, 992], [992, 993], [993, 994], [994, 995], [995, 996], [996, 997], [997, 998], [998, 999], [999, 1000]]]]
for roadPaths in final:
home = roadPaths[0]
school = roadPaths[1]
cool = roadPaths[2]
alrd = []
graph = {}
for val in cool:
n1 = val[0]
n2 = val[1]
if n1 not in alrd:
alrd.append(n1)
graph[n1] = [n2]
else:
graph[n1].append(n2)
if n2 not in alrd:
alrd.append(n2)
graph[n2] = [n1]
else:
graph[n2].append(n2)
notAllowed = []
lis = list(graph.keys())
path = breadthfirstsearch(graph, home, school)
for i in lis:
notAllowed.append([i, i])
for key, val in graph.items():
for v in val:
n = sorted([key, v])
if n not in notAllowed:
notAllowed.append(n)
for i in range(len(path)-2):
for j in range(i+2, len(path)):
n = sorted([path[i], path[j]])
notAllowed.append(n)
roads = 0
for i in lis:
for j in lis:
if sorted([i, j]) not in notAllowed:
notAllowed.append(sorted([i, j]))
roads += 1
print(roads)
And everything runs fine until it gets to the last section:
roads = 0
for i in lis:
for j in lis:
if sorted([i, j]) not in notAllowed:
notAllowed.append(sorted([i, j]))
roads += 1
Because there are 1000 elements in lis this loop basically executes 1,000,000 times which makes just this nested for loop insanely slow over half an hour to execute. Does anyone have any idea how I can speed this part up as when I submit the problem into the website I get a runtime error and I suspect that this part of the code is the problem.
The problem lies in the line if sorted([i, j]) not in notAllowed: which is very slow.
Indeed, notAllowed is a list that is growing very quickly and finding an element in a list is done in a linear time (the size of the list).
As a result, your loop as roughly a complexity of O(N^4) (with N=len(lis)).
It is possible to make this loop O(N^2) times faster using a set because finding an element in a set can be done in a constant time (in average).
Here is the result:
notAllowedSet = {tuple(e) for e in notAllowed}
roads = 0
for i in lis:
for j in lis:
elem = (min(i, j), max(i, j))
if elem not in notAllowedSet:
notAllowed.append(list(elem))
notAllowedSet.add(elem)
roads += 1
Please note that since sets cannot hash list-based pairs, I used tuple-based pairs that can be successfully hashed (using a tuple-typed pairs in notAllowed should also be faster than list-based pairs).
Additionally, sets may not preserve the ordering (regarding your version of python) and I do not know if the ordering of notAllowed is important. If the ordering of notAllowed is not important, you can remove notAllowed and only work on notAllowedSet. In such a case, it is probably possible to improve a lot the algorithm (by sorting lis and avoiding a relatively costly set check).

Categories

Resources