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

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

提交回复
热议问题