Python:如何在两个值之间切换

Yng*_*gve 33 python toggle

我想在Python中的两个值之间切换,即介于0和1之间.

例如,当我第一次运行一个函数时,它产生数字0.下一次,它产生1.第三次它回到零,依此类推.

对不起,如果这没有意义,但有没有人知道这样做的方法?

Pla*_*ure 57

用途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.
Run Code Online (Sandbox Code Playgroud)

请注意,如果你需要一个比[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, ...
Run Code Online (Sandbox Code Playgroud)

  • 不,我的意思是`myIterator = itertools.cycle(range(2))`单独工作.您不需要myFuncGenerator. (6认同)
  • 为什么不直接使用`itertools.cycle(range(2))`? (2认同)
  • 在 Python 2.6 或更高版本中,您应该使用 `next(x)` 而不是 `x.next()` - 通过此更改,此答案中的代码也适用于 Python 3.x。 (2认同)

g.d*_*d.c 45

你可以使用这样的生成器来完成它:

>>> def alternate():
...   while True:
...     yield 0
...     yield 1
...
>>>
>>> alternator = alternate()
>>>
>>> alternator.next()
0
>>> alternator.next()
1
>>> alternator.next()
0
Run Code Online (Sandbox Code Playgroud)

  • `itertools.cycle()`适用于任何可迭代的已知值.我没有看到`itertools`解决方案或者这个解决方案在运行时如何具有其生成器开关值,但我确信这样的事情并不困难. (4认同)

Hug*_*ell 18

您可能会发现创建一个像这样的函数别名很有用:

import itertools
myfunc = itertools.cycle([0,1]).next
Run Code Online (Sandbox Code Playgroud)

然后

myfunc()    # -> returns 0
myfunc()    # -> returns 1
myfunc()    # -> returns 0
myfunc()    # -> returns 1
Run Code Online (Sandbox Code Playgroud)


Lev*_*von 17

你可以使用mod(%)运算符.

count = 0  # initialize count once
Run Code Online (Sandbox Code Playgroud)

然后

count = (count + 1) % 2
Run Code Online (Sandbox Code Playgroud)

每次执行此语句时,都会将count的值切换为0到1之间的值.这种方法的优点是,您可以从操作员使用的值的0 - (n-1)位置循环显示一系列值(如果需要).此技术不依赖于任何Python特定的功能/库.n%

例如,

count = 0

for i in range(5):
     count = (count + 1) % 2
     print count
Run Code Online (Sandbox Code Playgroud)

得到:

1
0
1
0
1
Run Code Online (Sandbox Code Playgroud)


mgi*_*son 9

在python中,True和False 是整数(分别为1和0).您可以使用布尔值(True或False)和not运算符:

var = not var
Run Code Online (Sandbox Code Playgroud)

当然,如果你想在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
Run Code Online (Sandbox Code Playgroud)

  • 过于复杂......强烈建议调查`itertools`. (4认同)

Mar*_*cin 8

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
Run Code Online (Sandbox Code Playgroud)

  • 同乡`itertools`坚定的+1 :-) (2认同)

Set*_*hot 6

var = 1
var = 1 - var
Run Code Online (Sandbox Code Playgroud)

这是官方的棘手方法;)

  • @PlatinumAzure 不,不会。也许您将其读作“var = var - 1”?可能不是很Pythonic,但比使用“%”更好。它还适用于在任意两个数值之间切换。例如,“var = 3 - var”可在“1”和“2”之间切换。 (3认同)

Ale*_*ain 6

使用xor作品,是一种在两个值之间切换的良好视觉方式.

count = 1
count = count ^ 1 # count is now 0
count = count ^ 1 # count is now 1
Run Code Online (Sandbox Code Playgroud)


Sha*_*hin 5

使用元组下标技巧:

value = (1, 0)[value]
Run Code Online (Sandbox Code Playgroud)

  • 真的。这个技巧主要用作三元运算符的替代方案,但恰好适用于 OP 的特定情况。只是想我会加入一些不同的东西;) (2认同)

ack*_*ack 5

要在两个任意(整数)值(例如 a 和 b)之间切换变量 x,请使用:

    # 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
Run Code Online (Sandbox Code Playgroud)

例子:

在 3 和 5 之间切换

    x = 3
    x = 8 - x  (now x == 5)
    x = 8 - x  (now x == 3)
    x = 8 - x  (now x == 5)
Run Code Online (Sandbox Code Playgroud)

这甚至适用于字符串(有点)。

    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')
Run Code Online (Sandbox Code Playgroud)