Python的:如何在两个值之间切换(Python: How to toggle between t

2019-06-25 10:30发布

我想在Python两个值之间切换,也就是0和1之间。

例如,当我运行的功能的第一时间,它产生数字0下一次,它产生1第三时间它回到零,并依此类推。

很抱歉,如果这没有任何意义,但没有人知道的方式做到这一点?

Answer 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, ...


Answer 2:

你可以做到与这样的发电机:

>>> def alternate():
...   while True:
...     yield 0
...     yield 1
...
>>>
>>> alternator = alternate()
>>>
>>> alternator.next()
0
>>> alternator.next()
1
>>> alternator.next()
0


Answer 3:

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

import itertools
myfunc = itertools.cycle([0,1]).next

然后

myfunc()    # -> returns 0
myfunc()    # -> returns 1
myfunc()    # -> returns 0
myfunc()    # -> returns 1


Answer 4:

你可以使用国防部( % )运营商。

count = 0  # initialize count once

然后

count = (count + 1) % 2

将在每次执行该语句时切换0和1之间计数的值。 这种方法的好处是,你可以循环值从一个序列(如果需要) 0 - (n-1)其中n是用您使用的值%操作。 这种技术不依赖于任何特定的Python功能/库。

例如,

count = 0

for i in range(5):
     count = (count + 1) % 2
     print count

得到:

1
0
1
0
1


Answer 5:

在Python,真假为整数 (1和0分别)。 你可以使用一个布尔(TRUE或FALSE)和不操作:

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


Answer 6:

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


Answer 7:

使用xor的作品,而且是两个值之间切换了良好的视觉方式。

count = 1
count = count ^ 1 # count is now 0
count = count ^ 1 # count is now 1


Answer 8:

var = 1
var = 1 - var

这是做它的官方取巧的方法;)



Answer 9:

使用元组标招:

value = (1, 0)[value]


Answer 10:

要切换的两个任意(整数)值,例如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

例:

图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')


Answer 11:

使用元组标是两个值之间切换一个好办法:

toggle_val = 1

toggle_val = (1,0)[toggle_val]

如果你缠此功能,你将有一个很好的交流开关。



Answer 12:

不使用任何内置的简单和通用的解决方案。 只要保持目前的元素和打印的轨道/返回另一个则更改当前元素的状态。

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

输出:
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



Answer 13:

如果一个变量是预先定义的,你想它的两个值之间切换,你可以使用一个。如果B,否则c形式:

variable = 'value1'
variable = 'value2' if variable=='value1' else 'value1'

此外,它的工作原理上的Python 2.5+和3.x

https://docs.python.org/3/reference/expressions.html#conditional-expressions



文章来源: Python: How to toggle between two values
标签: python toggle