[英]Nested loop in Python with index
I'm trying to create some for loop using this code:我正在尝试使用此代码创建一些 for 循环:
StaticSteps = [2, 3, 1]
RAngles = [85.6,80.5,76]
increement = 50
for i, StaticStep in enumerate(StaticSteps):
RAngle = RAngles[i]
count = 0
while count < StaticStep:
print(RAngle, increement)
print('')
count = count+1
The result that I got is:我得到的结果是:
85.6 50
85.6 50
80.5 50
80.5 50
80.5 50
76 50
However, I want to add another loop on the left side so that results become:但是,我想在左侧添加另一个循环,以便结果变为:
50 85.6 50
100 85.6 50
150 80.5 50
200 80.5 50
250 80.5 50
300 76 50
How could I fix the code?我该如何修复代码?
You don't need a new loop, rather a new variable.你不需要一个新的循环,而是一个新的变量。 You could do as follows:
你可以这样做:
StaticSteps = [2, 3, 1]
RAngles = [85.6,80.5,76]
increment = 50
newIncrement = increment
for i, StaticStep in enumerate(StaticSteps):
RAngle = RAngles[i]
count = 0
while count < StaticStep:
print(newIncrement, RAngle, increment)
print('')
count = count+1
newIncrement = newIncrement + increment
Here, I have included a new variable newIncrement
which is printed and incremented by 50 after each iteration.在这里,我包含了一个新变量
newIncrement
,它在每次迭代后打印并递增 50。
Output: Output:
>>> 50 85.6 50
100 85.6 50
150 80.5 50
200 80.5 50
250 80.5 50
300 76 50
cnt = 0
for i, step in enumerate(static_steps):
for _ in range(step):
print(f"{(increment*cnt)+increment} {r_angles[i]} {increment}")
cnt += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.