如何减少一个函数的多个for循环?

时间:2019-07-17 20:48:31

标签: python python-3.x

我有这段代码,可以正常运行,但是我正在尝试压缩代码,以免混乱。我正在考虑使用一个函数使其更简单,但不确定从哪里开始。或者,如果可能,还有其他建议而不是功能。

B = np.genfromtxt(filename, dtype=(int, int, float, float),delimiter = '', usecols = (0, 1, 2, 3))
#numpy genfromtxt runs two main loops. First loop converts each line of the file in a sequence of strings. The second loop converts each string to the appropriate data type.

C = int(len(B)/4) #size of each set

D = 0 #index of vals
for i in range(0, C):
    TinEpoch_N1[D] = (B[i][0]*1000+B[i][1]/1000)/1000
    Rx1_N1[D] = B[i][2]
    Rx6_N1[D] = B[i][3]
    D = D+1
    print(D)

D = 0 #index of vals
for i in range(C, 2*C):
    TinEpoch_S1[D] = (B[i][0]*1000+B[i][1]/1000)/1000
    Rx1_S1[D] = B[i][2]
    Rx6_S1[D] = B[i][3]
    D = D+1
    print(D)

D = 0 #index of vals
for i in range(2*C, 3*C):
    TinEpoch_N2[D] = (B[i][0]*1000+B[i][1]/1000)/1000
    Rx1_N2[D] = B[i][2]
    Rx6_N2[D] = B[i][3]
    D = D+1
    print(D)

D = 0 #index of values
for i in range(3*C, 4*C):
    TinEpoch_S2[D] = (B[i][0]*1000+B[i][1]/1000)/1000
    Rx1_S2[D] = B[i][2]
    Rx6_S2[D] = B[i][3]
    D = D+1
    print(D)

在整个文件中,最终结果在Y轴上显示空气质量和天空值的图形。

0 个答案:

没有答案