简体   繁体   English

如何在每次迭代中删除范围的第一个值?

[英]How to remove first value of range in each iteration?

I would like to use a for loop before while loop and define a range and in each iteration, I would like to remove the first value of for loop我想在 while 循环之前使用 for 循环并定义一个范围,并且在每次迭代中,我想删除 for 循环的第一个值

the results that I would like to get after first iteration is h will be in the range(1, 30), next iteration(2,30) and until the end.我想在第一次迭代后得到的结果是 h 将在范围(1, 30)、下一次迭代(2,30)和直到结束。 The reason is my while loop doesn't use other values and only use the first value, I would like to use all 30 numbers原因是我的 while 循环不使用其他值而只使用第一个值,我想使用所有 30 个数字

I am just guessing what you are trying to do so I won't write any code but a logic is simple: put the code you have inside another for loop with variable i - from 0 to 30, then in you current code change 0 to i.我只是在猜测您要做什么,所以我不会编写任何代码,但逻辑很简单:将您拥有的代码放入另一个带有变量 i 的 for 循环中 - 从 0 到 30,然后在您当前的代码中将 0 更改为一世。 This way, your inner loop will iterate from 0 to 30, then 1 to 30, 2 to 30 and so on.这样,您的内部循环将从 0 迭代到 30,然后从 1 到 30、2 到 30 等等。

If you have a range, or a list of numbers, you could simply declare a variable before the while loop, eg i = 0 , and in the while loop point it at the index of the number in the list that you want to work on.如果您有一个范围或一个数字列表,您可以简单地在 while 循环之前声明一个变量,例如i = 0 ,并在 while 循环中将其指向您想要处理的列表中数字的索引. At the end of the while loop, increment i .在 while 循环结束时,增加i

You'd have something like this:你会有这样的事情:

foo = [ 1, 2, 3, 4, 5 ]
i = 0
while i < len(foo) :
    number_to_process = foo[i]
    *operations on number_to_process*
    i += 1

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

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