How to Convert Number 1 to a Boolean in Python

python: convert a list of 1/0s to a list of boolean

In python 2

 bool_list = map(bool,int_list)

In python 3:

 bool_list = list(map(bool,int_list))

Converting from a string to boolean in Python

Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:

s == 'True'

Or to checks against a whole bunch of values:

s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']

Be cautious when using the following:

>>> bool("foo")
True
>>> bool("")
False

Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes.

What is the most Pythonic way to convert 0 and 1 strings to boolean?

There are many ways, but I would like to propose the following:

{'0': False, '1': True}[input_value]

This has the advantage of raising an exception if you ever get a value different to what you expect (because of a bug, a malfunction, an API change etc).

All the other options presented thus far will silently accept any string as input.

How to convert numbers into boolean in Julia?

The quick answer is that it seems like you want to test whether a value is non-zero or not and you can test if a number x is non-zero like this: x != 0. Whether a number is considered true or false depends on what language you're in, however; more on that below.

Having gotten the quick answer out of the way, we can talk about why. The docs say that false and true are numerically equal to zero and one. We can test this out ourselves:

julia> false == 0
true

julia> true == 1.0
true

You can see that the equality holds regardless of numerical type—integers or floats or wven more esoteric number types like complexes or rationals. We can also convert booleans to other types of numbers:

julia> Int(false)
0

julia> Float64(true)
1.0

We can convert those values back to booleans:

julia> Bool(0)
false

julia> Bool(1.0)
true

What you can't do is convert a number that isn't equal to zero or one to boolean:

julia> Bool(5)
ERROR: InexactError: Bool(5)

That is because the Bool type can only represent the values zero and one exactly and trying to convert any other numeric value to Bool will give an InexactError. This is the same error you get if you try to convert a float that isn't integer-valued to an integer type:

julia> Int(5.0)
5

julia> Int(5.5)
ERROR: InexactError: Int64(5.5)

Or an integer to a smaller type that isn't big enough to represent that value:

julia> Int8(123)
123

julia> Int8(1234)
ERROR: InexactError: trunc(Int8, 1234)

The exact same thing is happening here: Bool is not big enough to represent the value 5, so you get an error if you try to convert the value 5 to Bool.

The convention that many languages use for truthiness of numbers is that values that represent zero are falsey and values that represent no-zeros are truthy. Note that there is no sound mathematical reason for this: zero is not false and non-zero numbers are not true; it's just a convention that comes from the C programming language, which doesn't have a boolean type and uses this convention for treating integers as true/false in conditionals. This convention is far from universal, however, as there are popular languages that don't follow it: in Lisps and Ruby, for example, all numbers are truthy. I wrote a post recently on the Julia discourse forum about differing notions of truthiness in different languages and why Julia rejects truthiness and instead requires you to write out an explicit condition like comparison with zero for numbers, or non-emptiness for collections.

Since the condition you actually want to check is whether a number is equal to zero or not, that's just what you should do: explicitly compare the number to zero by doing x != 0. This will evaluate to a boolean value: true if x is non-zero and false if x is equal to zero. There's a predicate function that does this as well: iszero(x) checks if x is zero, which can be handy if you want to e.g. count how many zero or non-zero values there are in a collection:

julia> v = rand(-2:2, 100);

julia> count(iszero, v)
18

julia> count(!iszero, v)
82

how to convert number < 1 from string to float python

Using str.replace

Ex:

a = '0,0127'
print(float(a.replace(",", ".")))

Output:

0.0127


Related Topics



Leave a reply



Submit