I would like to convert a list of integer 1s and 0s which represent a binary number to an int.
something along the lines of:
>>> [1,1,0,1].toint()
would give an output of 13
Strings are unnecessary here:
>>> l = [1,1,0,1]
>>>
>>> sum(j<<i for i,j in enumerate(reversed(l)))
13
Relevant documentation:
sum()
enumerate()
reversed()
You can do:
>>> int(''.join(map(str, my_list)), 2)
5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With