I have the following code and my goal is to populate a 10x48 array in python. The data however depends on a dataset which deserves a particular spot in the array. So I've been using if statements for each compute node to plot it but I want to know how to make it faster both in code and memory using nested for loops.
There are 48 columns and 10 rows.
this will continue until the final row where the last numbers will rotate between 36 37 38 and 39
so compute 1-1-36 compute 1-1-37 compute 1-1-38 compute 1-1-39 compute 1-2-36 all the way till 1-12-39
The following code represents the if statements I started but need a more efficient way so I can save memory and get rid of any mistakes I might make in copying and pasting.
temp = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
for i in linesAtTime:
if i[0] == "compute-1-1-0.localdomain":
temp[0][0]=float(i[4])
elif i[0] == "compute-1-1-1.localdomain":
temp[0][1] = float(i[4])
elif i[0] == "compute-1-1-2.localdomain":
temp[0][2] = float(i[4])
elif i[0] == "compute-1-1-3.localdomain":
temp[0][3] = float(i[4])
elif i[0] == "compute-1-2-0.localdomain":
temp[0][4] = float(i[4])
elif i[0] == "compute-1-2-1.localdomain":
temp[0][5] = float(i[4])
elif i[0] == "compute-1-2-2.localdomain":
temp[0][6] = float(i[4])
elif i[0] == "compute-1-2-3.localdomain":
temp[0][7] = float(i[4])
elif i[0] == "compute-1-3-0.localdomain":
temp[0][8] = float(i[4])
elif i[0] == "compute-1-3-1.localdomain":
temp[0][9] = float(i[4])
elif i[0] == "compute-1-3-2.localdomain":
temp[0][10] = float(i[4])
elif i[0] == "compute-1-3-3.localdomain":
temp[0][11] = float(i[4])
elif i[0] == "compute-1-4-0.localdomain":
temp[0][12] = float(i[4])
elif i[0] == "compute-1-4-1.localdomain":
temp[0][13] = float(i[4])
elif i[0] == "compute-1-4-2.localdomain":
temp[0][14] = float(i[4])
elif i[0] == "compute-1-4-3.localdomain":
temp[0][15] = float(i[4])
elif i[0] == "compute-1-5-0.localdomain":
temp[0][16] = float(i[4])
elif i[0] == "compute-1-5-1.localdomain":
temp[0][17] = float(i[4])
elif i[0] == "compute-1-5-2.localdomain":
temp[0][18] = float(i[4])
elif i[0] == "compute-1-5-3.localdomain":
temp[0][19] = float(i[4])
elif i[0] == "compute-1-6-0.localdomain":
temp[0][20] = float(i[4])
elif i[0] == "compute-1-6-1.localdomain":
temp[0][21] = float(i[4])
elif i[0] == "compute-1-6-2.localdomain":
temp[0][22] = float(i[4])
elif i[0] == "compute-1-6-3.localdomain":
temp[0][23] = float(i[4])
elif i[0] == "compute-1-7-0.localdomain":
temp[0][24] = float(i[4])
elif i[0] == "compute-1-7-1.localdomain":
temp[0][25] = float(i[4])
elif i[0] == "compute-1-7-2.localdomain":
temp[0][26] = float(i[4])
elif i[0] == "compute-1-7-3.localdomain":
temp[0][27] = float(i[4])
elif i[0] == "compute-1-8-0.localdomain":
temp[0][28] = float(i[4])
elif i[0] == "compute-1-8-1.localdomain":
temp[0][29] = float(i[4])
elif i[0] == "compute-1-8-2.localdomain":
temp[0][30] = float(i[4])
elif i[0] == "compute-1-8-3.localdomain":
temp[0][31] = float(i[4])
elif i[0] == "compute-1-9-0.localdomain":
temp[0][32] = float(i[4])
elif i[0] == "compute-1-9-1.localdomain":
temp[0][33] = float(i[4])
elif i[0] == "compute-1-9-2.localdomain":
temp[0][34] = float(i[4])
elif i[0] == "compute-1-9-3.localdomain":
temp[0][35] = float(i[4])
elif i[0] == "compute-1-10-0.localdomain":
temp[0][36] = float(i[4])
elif i[0] == "compute-1-10-1.localdomain":
temp[0][37] = float(i[4])
elif i[0] == "compute-1-10-2.localdomain":
temp[0][38] = float(i[4])
elif i[0] == "compute-1-10-3.localdomain":
temp[0][39] = float(i[4])
elif i[0] == "compute-1-11-0.localdomain":
temp[0][40] = float(i[4])
elif i[0] == "compute-1-11-0.localdomain":
temp[0][41] = float(i[4])
elif i[0] == "compute-1-11-1.localdomain":
temp[0][42] = float(i[4])
elif i[0] == "compute-1-11-2.localdomain":
temp[0][43] = float(i[4])
elif i[0] == "compute-1-11-3.localdomain":
temp[0][44] = float(i[4])
elif i[0] == "compute-1-12-0.localdomain":
temp[0][45] = float(i[4])
elif i[0] == "compute-1-12-1.localdomain":
temp[0][46] = float(i[4])
elif i[0] == "compute-1-12-2.localdomain":
temp[0][47] = float(i[4])
elif i[0] == "compute-1-12-3.localdomain":
temp[0][48] = float(i[4])
elif i[0] == "compute-1-1-4.localdomain":
temp[1][0]=float(i[4])
elif i[0] == "compute-1-1-5.localdomain":
temp[1][1] = float(i[4])
elif i[0] == "compute-1-1-6.localdomain":
temp[1][2] = float(i[4])
elif i[0] == "compute-1-1-7.localdomain":
temp[1][3] = float(i[4])
elif i[0] == "compute-1-2-4.localdomain":
temp[1][4] = float(i[4])
elif i[0] == "compute-1-2-5.localdomain":
temp[1][5] = float(i[4])
elif i[0] == "compute-1-2-6.localdomain":
temp[1][6] = float(i[4])
elif i[0] == "compute-1-2-7.localdomain":
temp[1][7] = float(i[4])
elif i[0] == "compute-1-3-4.localdomain":
temp[1][8] = float(i[4])
elif i[0] == "compute-1-3-5.localdomain":
temp[1][9] = float(i[4])
elif i[0] == "compute-1-3-6.localdomain":
temp[1][10] = float(i[4])
elif i[0] == "compute-1-3-7.localdomain":
temp[1][11] = float(i[4])
elif i[0] == "compute-1-4-4.localdomain":
temp[1][12] = float(i[4])
elif i[0] == "compute-1-4-5.localdomain":
temp[1][13] = float(i[4])
elif i[0] == "compute-1-4-6.localdomain":
temp[1][14] = float(i[4])
elif i[0] == "compute-1-4-7.localdomain":
temp[1][15] = float(i[4])
elif i[0] == "compute-1-5-4.localdomain":
temp[1][16] = float(i[4])
elif i[0] == "compute-1-5-5.localdomain":
temp[1][17] = float(i[4])
elif i[0] == "compute-1-5-6.localdomain":
temp[1][18] = float(i[4])
elif i[0] == "compute-1-5-7.localdomain":
temp[1][19] = float(i[4])
elif i[0] == "compute-1-6-4.localdomain":
temp[1][20] = float(i[4])
elif i[0] == "compute-1-6-5.localdomain":
temp[1][21] = float(i[4])
elif i[0] == "compute-1-6-6.localdomain":
temp[1][22] = float(i[4])
elif i[0] == "compute-1-6-7.localdomain":
temp[1][23] = float(i[4])
elif i[0] == "compute-1-7-4.localdomain":
temp[1][24] = float(i[4])
elif i[0] == "compute-1-7-5.localdomain":
temp[1][25] = float(i[4])
elif i[0] == "compute-1-7-6.localdomain":
temp[1][26] = float(i[4])
elif i[0] == "compute-1-7-7.localdomain":
temp[1][27] = float(i[4])
elif i[0] == "compute-1-8-4.localdomain":
temp[1][28] = float(i[4])
elif i[0] == "compute-1-8-5.localdomain":
temp[1][29] = float(i[4])
elif i[0] == "compute-1-8-6.localdomain":
temp[1][30] = float(i[4])
elif i[0] == "compute-1-8-7.localdomain":
temp[1][31] = float(i[4])
elif i[0] == "compute-1-9-4.localdomain":
temp[1][32] = float(i[4])
elif i[0] == "compute-1-9-5.localdomain":
temp[1][33] = float(i[4])
elif i[0] == "compute-1-9-6.localdomain":
temp[1][34] = float(i[4])
elif i[0] == "compute-1-9-7.localdomain":
temp[1][35] = float(i[4])
elif i[0] == "compute-1-10-4.localdomain":
temp[1][36] = float(i[4])
elif i[0] == "compute-1-10-5.localdomain":
temp[1][37] = float(i[4])
elif i[0] == "compute-1-10-6.localdomain":
temp[1][38] = float(i[4])
elif i[0] == "compute-1-10-7.localdomain":
temp[1][39] = float(i[4])
elif i[0] == "compute-1-11-4.localdomain":
temp[1][40] = float(i[4])
elif i[0] == "compute-1-11-5.localdomain":
temp[1][41] = float(i[4])
elif i[0] == "compute-1-11-6.localdomain":
temp[1][42] = float(i[4])
elif i[0] == "compute-1-11-7.localdomain":
temp[1][43] = float(i[4])
elif i[0] == "compute-1-11-4.localdomain":
temp[1][44] = float(i[4])
elif i[0] == "compute-1-12-5.localdomain":
temp[1][45] = float(i[4])
elif i[0] == "compute-1-12-6.localdomain":
temp[1][46] = float(i[4])
elif i[0] == "compute-1-12-7.localdomain":
temp[1][47] = float(i[4])
elif i[0] == "compute-1-12-3.localdomain":
temp[1][48] = float(i[4])
I have attached a psuedoillustration of my plan
I would recommend sorting your data first and then populating your array. You would have to write your own sort method, because a straight string compare will not give you a good order.
This is super sloppy because I wasn't able to run or test this at all, but you can try something like this.
def main():
# ...
number_of_rows = 12
number_of_columns = 48
temp = [[0] * number_of_columns for _ in range(number_of_rows)]
lookup = {}
for row in range(number_of_rows):
for column in range(number_of_columns):
key = "compute-{}-{}-{}.localdomain".format(1, (column // 4) + 1, (column % 4) + (row * 4))
value = row, column
lookup.update(key, value)
for i in lines_at_time:
row, column = lookup[i[0]]
temp[row][column] = float(i[4])
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.