简体   繁体   English

如何在python中执行嵌套循环

[英]How to do nested loop in python

I would like to do nested loop in my programming. 我想在我的程序中做嵌套循环。 However, this code did not meet my expectation. 但是,此代码不符合我的期望。

X=[0,1,1,1,0]

length=len(X)
for i,val in enumerate(X):
    a=0
    count=0
    while (count<length):
        a=15+a
        print (a)
        HWPQ=np.matrix([[1, 0, 0, 0], [0, math.cos(4*math.radians(a)), 
        math.sin(4*math.radians(a)), 0], [0, math.sin(4 * math.radians(a)), - 
        math.cos(4 * math.radians(a)), 0], [0, 0, 0, -1]])
        result=HWPQ*val
        print (result)
        count=count +1
    print ("\n")

Supposedly, in this program, I would like to update the value of a for each element using the loop. 据推测,在这种程序中,我想更新的值a用于使用所述回路中的每个元件。 For example: 例如:

X=0 for a=0
X=1 for a=15
X=1 for a=30
X=1 for a=45
X=0 for a=60

The result should be, as I calculated it manually: 结果应该是,正如我手动计算的那样:

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.         0.         0.         0.       ]
 [ 0.         0.5        0.8660254  0.       ]
 [ 0.         0.8660254 -0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]
[[ 1.         0.         0.         0.       ]
 [ 0.        -0.5        0.8660254  0.       ]
 [ 0.         0.8660254  0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]
[[  1.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00  -1.00000000e+00   1.22464680e-16   0.00000000e+00]
 [  0.00000000e+00   1.22464680e-16   1.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00  -1.00000000e+00]]
 [[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

There seem to be a load of unnecessary variables in your code, like count , X , and i . 您的代码中似乎有很多不必要的变量,例如countXi Removing them and using a standard for loop, you get: 删除它们并使用标准的for循环,您将获得:

X=[0,1,1,1,0]
a=0
for val in X:
    print (a)
    HWPQ=np.matrix([
        [1, 0, 0, 0], 
        [0, math.cos(4*math.radians(a)), math.sin(4*math.radians(a)), 0], 
        [0, math.sin(4 * math.radians(a)), - math.cos(4 * math.radians(a)), 0],
        [0, 0, 0, -1]])
    result=HWPQ*val
    print (result)
    a=15+a
    print ("\n")

which creates the output that you want. 这将创建所需的输出。

0
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]


15
[[ 1.         0.         0.         0.       ]
 [ 0.         0.5        0.8660254  0.       ]
 [ 0.         0.8660254 -0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]


30
[[ 1.         0.         0.         0.       ]
 [ 0.        -0.5        0.8660254  0.       ]
 [ 0.         0.8660254  0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]


45
[[  1.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00  -1.00000000e+00   1.22464680e-16   0.00000000e+00]
 [  0.00000000e+00   1.22464680e-16   1.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00  -1.00000000e+00]]


60
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

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

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