简体   繁体   English

如何将以下 if 语句转换为更有效的嵌套 for 循环

[英]How do I turn the following if statements into a more efficient nested for loops

I have the following code and my goal is to populate a 10x48 array in python.我有以下代码,我的目标是在 python 中填充一个 10x48 数组。 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.因此,我一直在为每个计算节点使用 if 语句到 plot 它,但我想知道如何使用嵌套的 for 循环在代码和 memory 中使其更快。

There are 48 columns and 10 rows.有 48 列和 10 行。

  • The 1st row each compute node will need to be in the range compute 1-1-0 compute 1-1-1 compute 1-1-2 compute 1-1-3 compute 1-2-0 all the way till 1-12-3每个计算节点的第一行需要在范围内计算 1-1-0 计算 1-1-1 计算 1-1-2 计算 1-1-3 计算 1-2-0 一直到 1-12 -3
  • the next row will have the same middle values but the last digits will now be using the numbers 4 5 6 and 7, so for example it will become compute 1-1-4 compute 1-1-5 compute 1-1-6 compute 1-1-7 compute 1-2-4 all the way till 1-12-7下一行将具有相同的中间值,但最后一位现在将使用数字 4 5 6 和 7,因此例如它将变为计算 1-1-4 计算 1-1-5 计算 1-1-6 计算1-1-7 一直计算 1-2-4 直到 1-12-7

this will continue until the final row where the last numbers will rotate between 36 37 38 and 39这将持续到最后一行,最后一个数字将在 36 37 38 和 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所以计算 1-1-36 计算 1-1-37 计算 1-1-38 计算 1-1-39 计算 1-2-36 一直到 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.以下代码代表我开始的 if 语句,但需要一种更有效的方法,以便我可以保存 memory 并摆脱我在复制和粘贴时可能犯的任何错误。

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, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM