Ray*_*ger 249
如果值是布尔值,则最快的方法是使用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)
如果值在0和1之间切换,则可以使用按位异或 -:
>>> 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.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__
ren*_*ger 29
我一直用:
p^=True
Run Code Online (Sandbox Code Playgroud)
如果p是布尔值,则在true和false之间切换.
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与互换True和False,所以就否定它:
toggle = not toggle
Run Code Online (Sandbox Code Playgroud)
但是如果您使用两个任意值,请使用内联if:
toggle = 'a' if toggle == 'b' else 'b'
Run Code Online (Sandbox Code Playgroud)
dan*_*era 11
三角函数方法,仅仅因为sin和cos函数很酷.
>>> 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)
令人惊讶的是没有人提到好的旧师模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)
切换的一种方法是使用多重赋值
>>> 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)
使用异常处理程序
>>> 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)
在 1 和 0 之间切换的最简单方法是从 1 中减去。
def toggle(value):
return 1 - value
Run Code Online (Sandbox Code Playgroud)