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

后端 未结 9 1056
情歌与酒
情歌与酒 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:26

    If B is a Boolean array, write

    B = B*1
    

    (A bit code golfy.)

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

    only with this:

    const a = true; const b = false;

    console.log(+a);//1 console.log(+b);//0

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

    bool to int: x = (x == 'true') + 0

    Now the x contains 1 if x == 'true' else 0.

    Note: x == 'true' will return bool which then will be typecasted to int having value (1 if bool value is True else 0) when added with 0.

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