如何在Python中切换值

118 python python-3.x

0和之间切换的最有效方法是什么1

Ray*_*ger 249

解决方案使用NOT

如果值是布尔值,则最快的方法是使用not运算符:

>>> x = True
>>> x = not x        # toggle
>>> x
False
>>> x = not x        # toggle
>>> x
True
>>> x = not x        # toggle
>>> x
False
Run Code Online (Sandbox Code Playgroud)

使用减法的解决方案

如果值是数字,则从总计中减去是一种简单快捷的切换值的方法:

>>> A = 5
>>> B = 3
>>> total = A + B
>>> x = A
>>> x = total - x    # toggle
>>> x
3
>>> x = total - x    # toggle
>>> x
5
>>> x = total - x    # toggle
>>> x
3
Run Code Online (Sandbox Code Playgroud)

使用XOR的解决方案

如果值在01之间切换,则可以使用按位异或 -:

>>> x = 1
>>> x ^= 1
>>> x
0
>>> x ^= 1
>>> x
1
Run Code Online (Sandbox Code Playgroud)

该技术推广到任何一对整数.xor-by-one步骤被替换为xor-by-precomputed-constant:

>>> A = 205
>>> B = -117
>>> t = A ^ B        # precomputed toggle constant
>>> x = A
>>> x ^= t           # toggle
>>> x
-117
>>> x ^= t           # toggle
>>> x
205
>>> x ^= t           # toggle
>>> x
-117
Run Code Online (Sandbox Code Playgroud)

(这个想法是由Nick Coghlan提交的,后来由@zxxc推广.)

解决方案使用字典

如果值是可清除的,则可以使用字典:

>>> A = 'xyz'
>>> B = 'pdq'
>>> d = {A:B, B:A}
>>> x = A
>>> x = d[x]         # toggle
>>> x
'pdq'
>>> x = d[x]         # toggle
>>> x
'xyz'
>>> x = d[x]         # toggle
>>> x
'pdq'
Run Code Online (Sandbox Code Playgroud)

解决方案使用条件表达式

最慢的方法是使用条件表达式:

>>> A = [1,2,3]
>>> B = [4,5,6]
>>> x = A
>>> x = B if x == A else A
>>> x
[4, 5, 6]
>>> x = B if x == A else A
>>> x
[1, 2, 3]
>>> x = B if x == A else A
>>> x
[4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

使用itertools的解决方案

如果您有两个以上的值,则itertools.cycle()函数提供了一种在连续值之间切换的通用快速方法:

>>> import itertools
>>> toggle = itertools.cycle(['red', 'green', 'blue']).next
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
Run Code Online (Sandbox Code Playgroud)

请注意,在Python 3中,next()方法已更改为__next__(),因此第一行现在将写为toggle = itertools.cycle(['red', 'green', 'blue']).__next__

  • 可以推广XOR示例以使用"x = x ^(a ^ b)"在值"a"和"b"之间切换. (6认同)
  • @labarna在Python 3中,`.next()`已被全局`next()`函数取代.上面的例子是:`toggle = itertools.cycle(...); 下一个(切换)` (2认同)
  • `toggle = itertools.cycle(['red','green','blue'])``next(toggle)` (2认同)
  • 不要将其称为“toggle”,而是将其称为“color”,这样您就可以说“next(color)”来获取...当然是下一个颜色:) (2认同)

ren*_*ger 29

我一直用:

p^=True
Run Code Online (Sandbox Code Playgroud)

如果p是布尔值,则在true和false之间切换.

  • 这是XOR运算符. (3认同)
  • @mix3d 准确地说,它是“按位异或”(与“逻辑异或”相反)- https://wiki.python.org/moin/BitwiseOperators。逻辑异或 [没有](/sf/ask/30298971/) 通常是 Python 中的特定运算符,但您可以发现它已实现 [在某些特殊情况下](https://docs.python .org/3/search.html?q=logical_xor) 就像在十进制模块中一样。 (2认同)

Abh*_*jit 22

这是另一种非直观的方式.美丽是你可以循环多个值而不仅仅是两个[0,1]

对于两个值(切换)

>>> x=[1,0]
>>> toggle=x[toggle]
Run Code Online (Sandbox Code Playgroud)

对于多个值(比如4)

>>> x=[1,2,3,0]
>>> toggle=x[toggle]
Run Code Online (Sandbox Code Playgroud)

我没想到这个解决方案也几乎是最快的

>>> stmt1="""
toggle=0
for i in xrange(0,100):
    toggle = 1 if toggle == 0 else 0
"""
>>> stmt2="""
x=[1,0]
toggle=0
for i in xrange(0,100):
    toggle=x[toggle]
"""
>>> t1=timeit.Timer(stmt=stmt1)
>>> t2=timeit.Timer(stmt=stmt2)
>>> print "%.2f usec/pass" % (1000000 * t1.timeit(number=100000)/100000)
7.07 usec/pass
>>> print "%.2f usec/pass" % (1000000 * t2.timeit(number=100000)/100000)
6.19 usec/pass
stmt3="""
toggle = False
for i in xrange(0,100):
    toggle = (not toggle) & 1
"""
>>> t3=timeit.Timer(stmt=stmt3)
>>> print "%.2f usec/pass" % (1000000 * t3.timeit(number=100000)/100000)
9.84 usec/pass
>>> stmt4="""
x=0
for i in xrange(0,100):
    x=x-1
"""
>>> t4=timeit.Timer(stmt=stmt4)
>>> print "%.2f usec/pass" % (1000000 * t4.timeit(number=100000)/100000)
6.32 usec/pass
Run Code Online (Sandbox Code Playgroud)


Ble*_*der 18

not运营商否定你的变量(将其转换成一个布尔值,如果它是不是已经一个).您可以或许1,并0与互换TrueFalse,所以就否定它:

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

但是如果您使用两个任意值,请使用内联if:

toggle = 'a' if toggle == 'b' else 'b'
Run Code Online (Sandbox Code Playgroud)


M S*_*M S 14

在1到0之间,这样做

1-x 
Run Code Online (Sandbox Code Playgroud)

x可以取1或0


dan*_*era 11

三角函数方法,仅仅因为sincos函数很酷.

在此输入图像描述

>>> import math
>>> def generator01():
...     n=0
...     while True:
...         yield abs( int( math.cos( n * 0.5 * math.pi  ) ) )
...         n+=1
... 
>>> g=generator01() 
>>> g.next()
1
>>> g.next()
0
>>> g.next()
1
>>> g.next()
0
Run Code Online (Sandbox Code Playgroud)

  • @RishabhAgrahari,是的,我是[Raymond Hettinger的挑战赛冠军](https://twitter.com/raymondh/status/887349008619810816);) (2认同)

Yau*_*ich 7

令人惊讶的是没有人提到好的旧师模2:

In : x = (x + 1)  % 2 ; x
Out: 1

In : x = (x + 1)  % 2 ; x
Out: 0

In : x = (x + 1)  % 2 ; x
Out: 1

In : x = (x + 1)  % 2 ; x
Out: 0
Run Code Online (Sandbox Code Playgroud)

注意它等价x = x - 1,但模数技术的优点是组的大小或间隔的长度可以大于2个元素,因此给你一个类似于循环的循环交错方案.

现在只有2,切换可以更短(使用逐位运算符):

x = x ^ 1
Run Code Online (Sandbox Code Playgroud)


hug*_*o24 6

切换的一种方法是使用多重赋值

>>> a = 5
>>> b = 3

>>> t = a, b = b, a
>>> t[0]
3

>>> t = a, b = b, a
>>> t[0]
5
Run Code Online (Sandbox Code Playgroud)

使用itertools:

In [12]: foo = itertools.cycle([1, 2, 3])

In [13]: next(foo)
Out[13]: 1

In [14]: next(foo)
Out[14]: 2

In [15]: next(foo)
Out[15]: 3

In [16]: next(foo)
Out[16]: 1

In [17]: next(foo)
Out[17]: 2
Run Code Online (Sandbox Code Playgroud)


dan*_*era 6

使用异常处理程序

>>> def toogle(x):
...     try:
...         return x/x-x/x
...     except  ZeroDivisionError:
...         return 1
... 
>>> x=0
>>> x=toogle(x)
>>> x
1
>>> x=toogle(x)
>>> x
0
>>> x=toogle(x)
>>> x
1
>>> x=toogle(x)
>>> x
0
Run Code Online (Sandbox Code Playgroud)

好吧,我是最差的:

在此输入图像描述

import math
import sys

d={1:0,0:1}
l=[1,0]

def exception_approach(x):
    try:
        return x/x-x/x
    except  ZeroDivisionError:
        return 1

def cosinus_approach(x):
    return abs( int( math.cos( x * 0.5 * math.pi  ) ) )

def module_approach(x):
    return  (x + 1)  % 2

def subs_approach(x):
    return  x - 1

def if_approach(x):
    return 0 if x == 1 else 1

def list_approach(x):
    global l
    return l[x]

def dict_approach(x):
    global d
    return d[x]

def xor_approach(x):
    return x^1

def not_approach(x):
    b=bool(x)
    p=not b
    return int(p)

funcs=[ exception_approach, cosinus_approach, dict_approach, module_approach, subs_approach, if_approach, list_approach, xor_approach, not_approach ]

f=funcs[int(sys.argv[1])]
print "\n\n\n", f.func_name
x=0
for _ in range(0,100000000):
    x=f(x)
Run Code Online (Sandbox Code Playgroud)


Bun*_*bit 5

在 1 和 0 之间切换的最简单方法是从 1 中减去。

def toggle(value):
    return 1 - value
Run Code Online (Sandbox Code Playgroud)