How to convert ‘false’ to 0 and ‘true’ to 1 in Python

后端 未结 9 1055
情歌与酒
情歌与酒 2021-01-29 22:55

Is there a way to convert true of type unicode to 1 and false of type unicode to 0 (in Python)?

For example: x

相关标签:
9条回答
  • 2021-01-29 23:05

    If you need a general purpose conversion from a string which per se is not a bool, you should better write a routine similar to the one depicted below. In keeping with the spirit of duck typing, I have not silently passed the error but converted it as appropriate for the current scenario.

    >>> def str2bool(st):
    try:
        return ['false', 'true'].index(st.lower())
    except (ValueError, AttributeError):
        raise ValueError('no Valid Conversion Possible')
    
    
    >>> str2bool('garbaze')
    
    Traceback (most recent call last):
      File "<pyshell#106>", line 1, in <module>
        str2bool('garbaze')
      File "<pyshell#105>", line 5, in str2bool
        raise TypeError('no Valid COnversion Possible')
    TypeError: no Valid Conversion Possible
    >>> str2bool('false')
    0
    >>> str2bool('True')
    1
    
    0 讨论(0)
  • 2021-01-29 23:05

    Any of the following will work:

    s = "true"
    
    (s == 'true').real
    1
    
    (s == 'false').real
    0
    
    (s == 'true').conjugate()    
    1
    
    (s == '').conjugate()
    0
    
    (s == 'true').__int__()
    1
    
    (s == 'opal').__int__()    
    0
    
    
    def as_int(s):
        return (s == 'true').__int__()
    
    >>>> as_int('false')
    0
    >>>> as_int('true')
    1
    
    0 讨论(0)
  • 2021-01-29 23:07

    You can use x.astype('uint8') where x is your Boolean array.

    0 讨论(0)
  • 2021-01-29 23:13

    +(False) converts to 0 and +(True) converts to 1

    0 讨论(0)
  • 2021-01-29 23:14

    Use int() on a boolean test:

    x = int(x == 'true')
    

    int() turns the boolean into 1 or 0. Note that any value not equal to 'true' will result in 0 being returned.

    0 讨论(0)
  • 2021-01-29 23:19

    Here's a yet another solution to your problem:

    def to_bool(s):
        return 1 - sum(map(ord, s)) % 2
        # return 1 - sum(s.encode('ascii')) % 2  # Alternative for Python 3
    

    It works because the sum of the ASCII codes of 'true' is 448, which is even, while the sum of the ASCII codes of 'false' is 523 which is odd.


    The funny thing about this solution is that its result is pretty random if the input is not one of 'true' or 'false'. Half of the time it will return 0, and the other half 1. The variant using encode will raise an encoding error if the input is not ASCII (thus increasing the undefined-ness of the behaviour).


    Seriously, I believe the most readable, and faster, solution is to use an if:

    def to_bool(s):
        return 1 if s == 'true' else 0
    

    See some microbenchmarks:

    In [14]: def most_readable(s):
        ...:     return 1 if s == 'true' else 0
    
    In [15]: def int_cast(s):
        ...:     return int(s == 'true')
    
    In [16]: def str2bool(s):
        ...:     try:
        ...:         return ['false', 'true'].index(s)
        ...:     except (ValueError, AttributeError):
        ...:         raise ValueError()
    
    In [17]: def str2bool2(s):
        ...:     try:
        ...:         return ('false', 'true').index(s)
        ...:     except (ValueError, AttributeError):
        ...:         raise ValueError()
    
    In [18]: def to_bool(s):
        ...:     return 1 - sum(s.encode('ascii')) % 2
    
    In [19]: %timeit most_readable('true')
    10000000 loops, best of 3: 112 ns per loop
    
    In [20]: %timeit most_readable('false')
    10000000 loops, best of 3: 109 ns per loop
    
    In [21]: %timeit int_cast('true')
    1000000 loops, best of 3: 259 ns per loop
    
    In [22]: %timeit int_cast('false')
    1000000 loops, best of 3: 262 ns per loop
    
    In [23]: %timeit str2bool('true')
    1000000 loops, best of 3: 343 ns per loop
    
    In [24]: %timeit str2bool('false')
    1000000 loops, best of 3: 325 ns per loop
    
    In [25]: %timeit str2bool2('true')
    1000000 loops, best of 3: 295 ns per loop
    
    In [26]: %timeit str2bool2('false')
    1000000 loops, best of 3: 277 ns per loop
    
    In [27]: %timeit to_bool('true')
    1000000 loops, best of 3: 607 ns per loop
    
    In [28]: %timeit to_bool('false')
    1000000 loops, best of 3: 612 ns per loop
    

    Notice how the if solution is at least 2.5x times faster than all the other solutions. It does not make sense to put as a requirement to avoid using ifs except if this is some kind of homework (in which case you shouldn't have asked this in the first place).

    0 讨论(0)
提交回复
热议问题