Python 将变量递增到100,然后再递减到0,然后在循环中再次递增
我只是想知道,将变量Python 将变量递增到100,然后再递减到0,然后在循环中再次递增,python,loops,variables,increment,itertools,Python,Loops,Variables,Increment,Itertools,我只是想知道,将变量x从0一直递增到100,然后当x达到100时,在一个循环中反复递减到0,然后再回到100,最“pythonic”的方法是什么 大概是这样的: x = 0 while True: if x < 100: x += 1 elif x == 100: x -= 1 x=0 尽管如此: 如果x=100: dr=False 其他: x-=1 如果x试试这个怎么样 x = 0 inc = 1 while True: # do your job here
x
从0一直递增到100,然后当x
达到100时,在一个循环中反复递减到0,然后再回到100,最“pythonic”的方法是什么
大概是这样的:
x = 0
while True:
if x < 100:
x += 1
elif x == 100:
x -= 1
x=0
尽管如此:
如果x<100:
x+=1
elif x==100:
x-=1
注:上述代码已被破坏,因此我的问题如下:)
最简单的方法是什么?不一定是最短的代码,也不需要一行代码,只需要一段非常好的代码。使用两个循环没有什么错
while True:
for x in range(100):
do_something(x)
for x in range(98, 0, -1):
do_something(x)
或者,您可以使用一个变量来跟踪您要走的方向:
delta = 1
x = 0
while True:
do_something(x)
x += delta
if x < 1 or x > 99:
delta = -delta
delta=1
x=0
尽管如此:
做某事(x)
x+=δ
如果x<1或x>99:
delta=-delta
首先,您的代码不工作:它将计数到100,然后在99和100之间交换。所以:1,2,3,…,99100,99100,99
实际上,最具python风格的方法可能是而不是增加/减少,而是使用两种范围s:
while True:
for x in range(101):
pass
#do something with x
for x in range(99,0,-1):
pass
#do something with x
或构造一个具有以下内容的无限生成器:
然后您可以将其用作:
for x in generator:
pass
#do something with x
Fr实例(我在这里使用2
,因为它使答案更简洁):
将产生:
0
1
2
1
0
1
2
1
0
循环将无限重复
但是,修复代码的一种方法是添加一个方向,但这可能不是非常符合Python的:
x=0
dr=True
尽管如此:
如果dr:
x+=1
如果x>=100:
dr=False
其他:
x-=1
如果x试试这个怎么样
x = 0
inc = 1
while True:
# do your job here
if x == 0:
inc = 1
elif x == 100:
inc = -1
x += inc
好吧,您当前的解决方案将无法正常工作-您将减记回99,然后在99和100之间循环
最简单的方法可能是添加一个方向
标志,例如:
x = 0
isIncreasing = True
while True:
if isIncreasing = True:
x += 1
if x == 100:
isIncreasing = False
else:
x -= 1
if x == 0:
isIncreasing = True
我很抱歉,在itertools中有一种更为“一行”的方法来实现这一点(参见上面的文章),但在您当前的情况下,这将是最“直接”的解决方案。当然,你真正想要的是一个生成器
我发现其中一个很简单。。。那么这个呢:
Python 2:
Python 3:
(编辑以删除一次性错误)既然没有其他人在做这件事,为什么不找点发电机的乐趣呢
def lazy_forever_and_ever(hi, lo, start=0, step=1):
x = start
vals = {hi: lo, lo: hi}
target=lo
while True:
if x == target:
target = vals[target]
if x >= target:
yield x
x -= step
elif x <= target:
yield x
x += step
if __name__ == '__main__':
_4eva = lazy_forever_and_ever(0, 10, 0, 1)
print([next(_4eva) for _ in range(20)])
# output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
def lazy_ever_和_ever(hi、lo、start=0、step=1):
x=开始
VAL={hi:lo,lo:hi}
目标=低
尽管如此:
如果x==目标:
目标=VAL[目标]
如果x>=目标:
产量x
x-=阶跃
elif x这不是你想要它做的elif x==100:
会将其减回到99,如果x<100:
为True
,则此时它将再次递增。在尝试优化之前,更重要的是要确保某些东西是有效的。是的,我意识到上面的代码不起作用。你最初的问题没有说明这一点。祝你好运,找到任何澄清,在什么是“pythonic”在什么是提供的答案。我会将功能性评级高于pythonicism(这是一个词吗?),但你看,你的范围不是对称的。你是对的。结果,迭代过早地返回。链接和循环[01100)
和[100,0)
应该更有效,看起来也更好;)@jonrsharpe:我认为它们不应该是对称的,否则100
和0
会被打印两次。使用两个范围很好-更像蟒蛇。谢谢你-是的,我喜欢这两个范围。我接受了这个答案,因为它对多个范围有很好的解释答案。事实上,我最喜欢这个方法,所以我改变了被接受的答案。
x = 0
inc = 1
while True:
# do your job here
if x == 0:
inc = 1
elif x == 100:
inc = -1
x += inc
x = 0
isIncreasing = True
while True:
if isIncreasing = True:
x += 1
if x == 100:
isIncreasing = False
else:
x -= 1
if x == 0:
isIncreasing = True
from itertools import *
ups_and_downs = cycle(chain(xrange(100), xrange(100, 0, -1)))
from itertools import *
ups_and_downs = cycle(chain(range(100), range(100, 0, -1)))
def lazy_forever_and_ever(hi, lo, start=0, step=1):
x = start
vals = {hi: lo, lo: hi}
target=lo
while True:
if x == target:
target = vals[target]
if x >= target:
yield x
x -= step
elif x <= target:
yield x
x += step
if __name__ == '__main__':
_4eva = lazy_forever_and_ever(0, 10, 0, 1)
print([next(_4eva) for _ in range(20)])
# output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]