【发布时间】:2022-03-11 01:02:33
【问题描述】:
我想在 Python 中的两个值之间切换,即 0 和 1 之间。
例如,当我第一次运行一个函数时,它产生数字 0。下一次,它产生 1。第三次它又回到零,依此类推。
对不起,如果这没有意义,但有人知道这样做的方法吗?
【问题讨论】:
我想在 Python 中的两个值之间切换,即 0 和 1 之间。
例如,当我第一次运行一个函数时,它产生数字 0。下一次,它产生 1。第三次它又回到零,依此类推。
对不起,如果这没有意义,但有人知道这样做的方法吗?
【问题讨论】:
使用itertools.cycle():
from itertools import cycle
myIterator = cycle(range(2))
myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 0
myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 1
# etc.
请注意,如果您需要比[0, 1] 更复杂的循环,则此解决方案将变得比此处发布的其他解决方案更具吸引力...
from itertools import cycle
mySmallSquareIterator = cycle(i*i for i in range(10))
# Will yield 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, ...
【讨论】:
itertools.cycle(range(2))?
myIterator = itertools.cycle(range(2)) 自己工作。你不需要 myFuncGenerator。
next(x) 而不是 x.next() - 通过此更改,此答案中的代码也可以在 Python 3.x 中使用。
您可以使用这样的生成器来完成此操作:
>>> def alternate():
... while True:
... yield 0
... yield 1
...
>>>
>>> alternator = alternate()
>>>
>>> alternator.next()
0
>>> alternator.next()
1
>>> alternator.next()
0
【讨论】:
itertools.cycle() 适用于任何可迭代的已知值。我看不出itertools 解决方案或这个解决方案如何在运行时拥有其生成器开关值,但我确信这样的事情并不难。
您可以使用 mod (%) 运算符。
count = 0 # initialize count once
然后
count = (count + 1) % 2
每次执行此语句时,都会在 0 和 1 之间切换 count 的值。这种方法的优势是您可以循环访问来自0 - (n-1) 的一系列值(如果需要),其中n 是您与% 运算符一起使用的值。而且这种技术不依赖于任何 Python 特定的功能/库。
例如
count = 0
for i in range(5):
count = (count + 1) % 2
print(count)
给予:
1
0
1
0
1
【讨论】:
i 和count 变量。
您可能会发现像这样创建函数别名很有用:
import itertools
myfunc = itertools.cycle([0,1]).next
然后
myfunc() # -> returns 0
myfunc() # -> returns 1
myfunc() # -> returns 0
myfunc() # -> returns 1
【讨论】:
在 python 中,True 和 False 是整数(分别为 1 和 0)。您可以使用布尔值(True 或 False)和 not 运算符:
var = not var
当然,如果你想在 0 和 1 以外的数字之间进行迭代,这个技巧就变得有点困难了。
将其打包成一个公认的丑陋函数:
def alternate():
alternate.x=not alternate.x
return alternate.x
alternate.x=True #The first call to alternate will return False (0)
mylist=[5,3]
print(mylist[alternate()]) #5
print(mylist[alternate()]) #3
print(mylist[alternate()]) #5
【讨论】:
itertools。
alternate.x(而不是使用默认技巧)..我不得不承认我用过一两次. (但只有一两次。)
from itertools import cycle
alternator = cycle((0,1))
next(alternator) # yields 0
next(alternator) # yields 1
next(alternator) # yields 0
next(alternator) # yields 1
#... forever
【讨论】:
var = 1
var = 1 - var
这是官方的棘手方法;)
【讨论】:
var = var - 1?可能不是很pythonic,但比使用% 更好。它也适用于在任意两个数值之间切换。例如,var = 3 - var 在1 和2 之间切换。
使用xor 有效,并且是在两个值之间切换的一种很好的视觉方式。
count = 1
count = count ^ 1 # count is now 0
count = count ^ 1 # count is now 1
【讨论】:
要在两个任意(整数)值之间切换变量 x, 例如a 和 b,使用:
# start with either x == a or x == b
x = (a + b) - x
# case x == a:
# x = (a + b) - a ==> x becomes b
# case x == b:
# x = (a + b) - b ==> x becomes a
例子:
在 3 和 5 之间切换
x = 3
x = 8 - x (now x == 5)
x = 8 - x (now x == 3)
x = 8 - x (now x == 5)
这甚至适用于字符串(有点)。
YesNo = 'YesNo'
answer = 'Yes'
answer = YesNo.replace(answer,'') (now answer == 'No')
answer = YesNo.replace(answer,'') (now answer == 'Yes')
answer = YesNo.replace(answer,'') (now answer == 'No')
【讨论】:
使用元组下标技巧:
value = (1, 0)[value]
【讨论】:
使用元组下标是在两个值之间切换的一种好方法:
toggle_val = 1
toggle_val = (1,0)[toggle_val]
如果你在这个周围包裹一个函数,你会有一个很好的交替开关。
【讨论】:
如果之前定义了一个变量并且您希望它在两个值之间切换,您可以使用 a if b else c 表单:
variable = 'value1'
variable = 'value2' if variable=='value1' else 'value1'
此外,它适用于 Python 2.5+ 和 3.x
请参阅 Python 3 文档中的 Expressions。
【讨论】:
简单通用的解决方案,不使用任何内置。只需跟踪当前元素并打印/返回另一个元素,然后更改当前元素状态。
a, b = map(int, raw_input("Enter both number: ").split())
flag = input("Enter the first value: ")
length = input("Enter Number of iterations: ")
for i in range(length):
print flag
if flag == a:
flag = b;
else:
flag = a
输入:
3 8
3
5
输出:
3
8
3
8
3
Means numbers to be toggled are 3 and 8
Second input, is the first value by which you want to start the sequence
And last input indicates the number of times you want to generate
【讨论】:
你可以用任何语言做一个很酷的方法:
variable = 0
variable = abs(variable - 1) // 1
variable = abs(variable - 1) // 0
【讨论】:
variable = 1 - variable 更酷 ;)