return不断变化的变量python
return constantly changing variable python
我正在尝试return一个不断变化的变量Python 3、变量在一个while循环中,计数到指定的数字,将其值变为零,然后重新开始。
(例如 1、2、3、4、5、0、1、2、3、4、5、0...)
这是我的代码:
global x
run = True
def count(l):
x = 0
global run
while run:
x += 1
if x == l+1:
x = 0
print(x)
当我 运行 count(5)
它 打印 想要的结果时,但是 1:我试图找到一种方法来不断地 运行 在后台循环,以及 2:变量需要 returned,而不是打印
GetCount()
并获得return想要的结果。
我试过将它添加到函数的末尾:
try:
return x
finally:
count(l)
但随后它崩溃了 比较期间超出了最大递归深度
所以我的问题是,1:有没有办法让 x 在不达到最大递归深度的情况下连续 returned?,以及 2:如果有,是否还有办法让 count()
在后台 运行ning,所以我可以获得 x 的当前值,如果是的话如何?
看起来 itertools.cycle
可以帮助:
import itertools as it
c = it.cycle(range(6))
for x in range(10):
print(next(c))
打印:
0
1
2
3
4
5
0
1
2
3
只需使用 next(c)
即可获得下一个数字。
print(next(c))
现在打印:
4
因为我们之前停在了3
。您可以设置您的 ID 值:
my_id = next(c)
你想要的是一个生成器并产生每个值:
def count(l):
x = 0
while True:
yield x
if x == l:
x = -1
x += 1
for i in count(5):
print(i)
你想要的是做一个生成器函数
It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.
Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.
如果您希望以某种方式计算一系列值,但又不需要一次全部计算,因为您只在特定时间需要一个 and/or,那么您可以创建一个生成器太多甚至无穷无尽。
生成器会记住是否在产生值的那一刻,并会从需要新值的那一刻起恢复计算。
@Mike Muller 已经为你的问题提供了完美的解决方案,那么再举一个例子,比如斐波那契数列,有无穷多个,但是你可以制作一个生成器,像这样为你提供所有这些数
def Fib():
Fn = 0
Fn1 = 1
while True:
yield Fn
Fn,Fn1 = Fn1, Fn+Fn1
这将在您要求时立即为您提供每个斐波那契数
>>> fibo=Fib()
>>> for i in range(10):
print("the",i,"-th fibonacci number is:",next(fibo))
the 0 -th fibonacci number is: 0
the 1 -th fibonacci number is: 1
the 2 -th fibonacci number is: 1
the 3 -th fibonacci number is: 2
the 4 -th fibonacci number is: 3
the 5 -th fibonacci number is: 5
the 6 -th fibonacci number is: 8
the 7 -th fibonacci number is: 13
the 8 -th fibonacci number is: 21
the 9 -th fibonacci number is: 34
>>> next(fibo) #10-th
55
我正在尝试return一个不断变化的变量Python 3、变量在一个while循环中,计数到指定的数字,将其值变为零,然后重新开始。 (例如 1、2、3、4、5、0、1、2、3、4、5、0...) 这是我的代码:
global x
run = True
def count(l):
x = 0
global run
while run:
x += 1
if x == l+1:
x = 0
print(x)
当我 运行 count(5)
它 打印 想要的结果时,但是 1:我试图找到一种方法来不断地 运行 在后台循环,以及 2:变量需要 returned,而不是打印
GetCount()
并获得return想要的结果。
我试过将它添加到函数的末尾:
try:
return x
finally:
count(l)
但随后它崩溃了 比较期间超出了最大递归深度
所以我的问题是,1:有没有办法让 x 在不达到最大递归深度的情况下连续 returned?,以及 2:如果有,是否还有办法让 count()
在后台 运行ning,所以我可以获得 x 的当前值,如果是的话如何?
看起来 itertools.cycle
可以帮助:
import itertools as it
c = it.cycle(range(6))
for x in range(10):
print(next(c))
打印:
0
1
2
3
4
5
0
1
2
3
只需使用 next(c)
即可获得下一个数字。
print(next(c))
现在打印:
4
因为我们之前停在了3
。您可以设置您的 ID 值:
my_id = next(c)
你想要的是一个生成器并产生每个值:
def count(l):
x = 0
while True:
yield x
if x == l:
x = -1
x += 1
for i in count(5):
print(i)
你想要的是做一个生成器函数
It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.
Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.
如果您希望以某种方式计算一系列值,但又不需要一次全部计算,因为您只在特定时间需要一个 and/or,那么您可以创建一个生成器太多甚至无穷无尽。
生成器会记住是否在产生值的那一刻,并会从需要新值的那一刻起恢复计算。
@Mike Muller 已经为你的问题提供了完美的解决方案,那么再举一个例子,比如斐波那契数列,有无穷多个,但是你可以制作一个生成器,像这样为你提供所有这些数
def Fib():
Fn = 0
Fn1 = 1
while True:
yield Fn
Fn,Fn1 = Fn1, Fn+Fn1
这将在您要求时立即为您提供每个斐波那契数
>>> fibo=Fib()
>>> for i in range(10):
print("the",i,"-th fibonacci number is:",next(fibo))
the 0 -th fibonacci number is: 0
the 1 -th fibonacci number is: 1
the 2 -th fibonacci number is: 1
the 3 -th fibonacci number is: 2
the 4 -th fibonacci number is: 3
the 5 -th fibonacci number is: 5
the 6 -th fibonacci number is: 8
the 7 -th fibonacci number is: 13
the 8 -th fibonacci number is: 21
the 9 -th fibonacci number is: 34
>>> next(fibo) #10-th
55