如何在Python中切换值

问题:如何在Python中切换值

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

What is the most efficient way to toggle between 0 and 1?


回答 0

使用NOT的解决方案

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

>>> x = True
>>> x = not x        # toggle
>>> x
False
>>> x = not x        # toggle
>>> x
True
>>> x = not x        # toggle
>>> x
False

用减法求解

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

>>> 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

使用XOR的解决方案

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

>>> x = 1
>>> x ^= 1
>>> x
0
>>> x ^= 1
>>> x
1

该技术可以推广到任意一对整数。异或一步被替换为预异常数:

>>> 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

(此想法由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'

使用条件表达式的解决方案

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

>>> 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]

使用itertools的解决方案

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

>>> import itertools
>>> toggle = itertools.cycle(['red', 'green', 'blue']).next
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'

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

Solution using NOT

If the values are boolean, the fastest approach is to use the not operator:

>>> x = True
>>> x = not x        # toggle
>>> x
False
>>> x = not x        # toggle
>>> x
True
>>> x = not x        # toggle
>>> x
False

Solution using subtraction

If the values are numerical, then subtraction from the total is a simple and fast way to toggle values:

>>> 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

Solution using XOR

If the value toggles between 0 and 1, you can use a bitwise exclusive-or:

>>> x = 1
>>> x ^= 1
>>> x
0
>>> x ^= 1
>>> x
1

The technique generalizes to any pair of integers. The xor-by-one step is replaced with a 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

(This idea was submitted by Nick Coghlan and later generalized by @zxxc.)

Solution using a dictionary

If the values are hashable, you can use a dictionary:

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

Solution using a conditional expression

The slowest way is to use a conditional expression:

>>> 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]

Solution using itertools

If you have more than two values, the itertools.cycle() function provides a generic fast way to toggle between successive values:

>>> import itertools
>>> toggle = itertools.cycle(['red', 'green', 'blue']).next
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'

Note that in Python 3 the next() method was changed to __next__(), so the first line would be now written as toggle = itertools.cycle(['red', 'green', 'blue']).__next__


回答 1

我一直使用:

p^=True

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

I always use:

p^=True

If p is a boolean, this switches between true and false.


回答 2

这是另一种不直观的方法。优点是您可以循环多个值,而不仅仅是两个[0,1]

对于两个值(切换)

>>> x=[1,0]
>>> toggle=x[toggle]

对于多个值(例如4)

>>> x=[1,2,3,0]
>>> toggle=x[toggle]

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

>>> 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

Here is another non intuitive way. The beauty is you can cycle over multiple values and not just two [0,1]

For Two values (toggling)

>>> x=[1,0]
>>> toggle=x[toggle]

For Multiple Values (say 4)

>>> x=[1,2,3,0]
>>> toggle=x[toggle]

I didn’t expect this solution to be almost the fastest too

>>> 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

回答 3

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

toggle = not toggle

但是,如果您使用两个任意值,请使用inline if

toggle = 'a' if toggle == 'b' else 'b'

The not operator negates your variable (converting it into a boolean if it isn’t already one). You can probably use 1 and 0 interchangeably with True and False, so just negate it:

toggle = not toggle

But if you are using two arbitrary values, use an inline if:

toggle = 'a' if toggle == 'b' else 'b'

回答 4

在1到0之间执行此操作

1-x 

x可以取1或0

Just between 1 and 0, do this

1-x 

x can take 1 or 0


回答 5

三角法,仅仅是因为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

Trigonometric approach, just because sin and cos functions are cool.

>>> 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

回答 6

令人惊讶的是,没有人提到好的旧除法模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

请注意,它等效于x = x - 1,但是取模技术的优点是组的大小或间隔的长度可以大于2个元素,从而为循环提供了类似于轮询交错的方案。

现在只需要2,切换就可以短一些(使用按位运算符):

x = x ^ 1

Surprisingly nobody mention good old division modulo 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

Note that it is equivalent to x = x - 1, but the advantage of modulo technique is that the size of the group or length of the interval can be bigger then just 2 elements, thus giving you a similar to round-robin interleaving scheme to loop over.

Now just for 2, toggling can be a bit shorter (using bit-wise operator):

x = x ^ 1

回答 7

一种切换方式是使用多重分配

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

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

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

使用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

one way to toggle is by using Multiple assignment

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

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

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

Using 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

回答 8

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

def toggle(value):
    return 1 - value

The easiest way to toggle between 1 and 0 is to subtract from 1.

def toggle(value):
    return 1 - value

回答 9

使用异常处理程序

>>> 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

好吧,我是最糟糕的:

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)

Using exception handler

>>> 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

Ok, I’m the worst:

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)

回答 10

怎么样一个假想的切换,存储不仅是当前切换,但与之相关的其他几个值?

toggle = complex.conjugate

在左侧存储任何+或-值,在右侧存储任何无符号的值:

>>> x = 2 - 3j
>>> toggle(x)
(2+3j)

零也起作用:

>>> y = -2 - 0j
>>> toggle(y)
(-2+0j)

轻松检索当前的切换值(TrueFalse表示+和-),LHS(实数)值或RHS(虚数)值:

>>> import math
>>> curr = lambda i: math.atan2(i.imag, -abs(i.imag)) > 0
>>> lhs = lambda i: i.real
>>> rhs = lambda i: abs(i.imag)
>>> x = toggle(x)
>>> curr(x)
True
>>> lhs(x)
2.0
>>> rhs(x)
3.0

轻松交换LHS和RHS(但请注意,两个值的符号一定不重要):

>>> swap = lambda i: i/-1j
>>> swap(2+0j)
2j
>>> swap(3+2j)
(2+3j)

轻松交换LHS和RHS 并同时切换:

>>> swaggle = lambda i: i/1j
>>> swaggle(2+0j)
-2j
>>> swaggle(3+2j)
(2-3j)

防止错误:

>>> toggle(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'conjugate' requires a 'complex' object but received a 'int'

对LHS和RHS进行更改:

>>> x += 1+2j
>>> x
(3+5j)

…但是要小心操作RHS:

>>> z = 1-1j
>>> z += 2j
>>> z
(1+1j) # whoops! toggled it!

How about an imaginary toggle that stores not only the current toggle, but a couple other values associated with it?

toggle = complex.conjugate

Store any + or – value on the left, and any unsigned value on the right:

>>> x = 2 - 3j
>>> toggle(x)
(2+3j)

Zero works, too:

>>> y = -2 - 0j
>>> toggle(y)
(-2+0j)

Easily retrieve the current toggle value (True and False represent + and -), LHS (real) value, or RHS (imaginary) value:

>>> import math
>>> curr = lambda i: math.atan2(i.imag, -abs(i.imag)) > 0
>>> lhs = lambda i: i.real
>>> rhs = lambda i: abs(i.imag)
>>> x = toggle(x)
>>> curr(x)
True
>>> lhs(x)
2.0
>>> rhs(x)
3.0

Easily swap LHS and RHS (but note that the sign of the both values must not be important):

>>> swap = lambda i: i/-1j
>>> swap(2+0j)
2j
>>> swap(3+2j)
(2+3j)

Easily swap LHS and RHS and also toggle at the same time:

>>> swaggle = lambda i: i/1j
>>> swaggle(2+0j)
-2j
>>> swaggle(3+2j)
(2-3j)

Guards against errors:

>>> toggle(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'conjugate' requires a 'complex' object but received a 'int'

Perform changes to LHS and RHS:

>>> x += 1+2j
>>> x
(3+5j)

…but be careful manipulating the RHS:

>>> z = 1-1j
>>> z += 2j
>>> z
(1+1j) # whoops! toggled it!

回答 11

变量a和b可以是任意两个值,例如0和1,或者117和711,或“ heads”和“ tails”。不使用数学运算,每次需要切换时仅快速交换值。

a = True   
b = False   

a,b = b,a   # a is now False
a,b = b,a   # a is now True

Variables a and b can be ANY two values, like 0 and 1, or 117 and 711, or “heads” and “tails”. No math is used, just a quick swap of the values each time a toggle is desired.

a = True   
b = False   

a,b = b,a   # a is now False
a,b = b,a   # a is now True

回答 12

我使用abs函数,在循环中非常有用

x = 1
for y in range(0, 3):
    x = abs(x - 1)

x将为0。

I use abs function, very useful on loops

x = 1
for y in range(0, 3):
    x = abs(x - 1)

x will be 0.


回答 13

让我们做一些框架黑客。按名称切换变量。注意:这可能不适用于每个Python运行时。

假设您有一个变量“ x”

>>> import inspect
>>> def toggle(var_name):
>>>     frame = inspect.currentframe().f_back
>>>     vars = frame.f_locals
>>>     vars[var_name] = 0 if vars[var_name] == 1 else 1

>>> x = 0
>>> toggle('x')
>>> x
1
>>> toggle('x')
>>> x
0

Let’s do some frame hacking. Toggle a variable by name. Note: This may not work with every Python runtime.

Say you have a variable “x”

>>> import inspect
>>> def toggle(var_name):
>>>     frame = inspect.currentframe().f_back
>>>     vars = frame.f_locals
>>>     vars[var_name] = 0 if vars[var_name] == 1 else 1

>>> x = 0
>>> toggle('x')
>>> x
1
>>> toggle('x')
>>> x
0

回答 14

如果要处理整数变量,则可以递增1并将其限制为0和1(mod)

X = 0  # or X = 1
X = (X + 1)%2

If you are dealing with an integer variable, you can increment 1 and limit your set to 0 and 1 (mod)

X = 0  # or X = 1
X = (X + 1)%2

回答 15

可以通过内联乘法在-1和+1之间切换。用于以“ Leibniz”方式(或类似方式)计算pi:

sign = 1
result = 0
for i in range(100000):
    result += 1 / (2*i + 1) * sign
    sign *= -1
print("pi (estimate): ", result*4)

Switching between -1 and +1 can be obtained by inline multiplication; used for calculation of pi the ‘Leibniz’ way (or similar):

sign = 1
result = 0
for i in range(100000):
    result += 1 / (2*i + 1) * sign
    sign *= -1
print("pi (estimate): ", result*4)

回答 16

您可以使用的indexlist秒。

def toggleValues(values, currentValue):
    return values[(values.index(currentValue) + 1) % len(values)]

> toggleValues( [0,1] , 1 )
> 0
> toggleValues( ["one","two","three"] , "one" )
> "two"
> toggleValues( ["one","two","three"] , "three")
> "one"

优点:无需其他库,自我解释代码并可以处理任意数据类型。

缺点:不重复保存。 toggleValues(["one","two","duped", "three", "duped", "four"], "duped") 永远会回来"three"

You can make use of the index of lists.

def toggleValues(values, currentValue):
    return values[(values.index(currentValue) + 1) % len(values)]

> toggleValues( [0,1] , 1 )
> 0
> toggleValues( ["one","two","three"] , "one" )
> "two"
> toggleValues( ["one","two","three"] , "three")
> "one"

Pros: No additional libraries, self.explanatory code and working with arbitrary data types.

Cons: not duplicate-save. toggleValues(["one","two","duped", "three", "duped", "four"], "duped") will always return "three"