How to toggle a value in Python

后端 未结 17 945
梦谈多话
梦谈多话 2020-12-07 07:46

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

相关标签:
17条回答
  • 2020-12-07 08:25

    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)
    
    0 讨论(0)
  • 2020-12-07 08:30

    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
    
    0 讨论(0)
  • 2020-12-07 08:30

    I use abs function, very useful on loops

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

    x will be 0.

    0 讨论(0)
  • 2020-12-07 08:33

    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
    
    0 讨论(0)
  • 2020-12-07 08:33

    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
    
    0 讨论(0)
提交回复
热议问题