哪个是执行这些嵌套循环的最有效方法?

时间:2016-05-23 08:20:45

标签: python

# uppercase the hostname and cleaning

hostname=$("echo "$1" | sed -re 's/.vdb.local//' | tr '[:lower:]' '[:upper:]'")

# create the command line to add to the command file
cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$2;$3;$4"

我遍历3D数组,如果任何值不等于1,我想将其更改为1.

1 个答案:

答案 0 :(得分:4)

如果您使用numpy,请写下:

arr[arr!=0] = 1

或者如果你只需要一个布尔数组:

result = arr!=0

另一方面,如果你有一份清单清单:

for plane in arr:
    for row in plane:
        row[:] = [int(item!=0) for item in row]