Reputation: 10575
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
Upvotes: 4
Views: 830
Reputation:
Look at this:
>>> x = [1,1,0,1]
>>> int("".join(map(str, x)), 2)
13
>>>
Upvotes: 3
Reputation: 129507
Strings are unnecessary here:
>>> l = [1,1,0,1]
>>>
>>> sum(j<<i for i,j in enumerate(reversed(l)))
13
Relevant documentation:
Upvotes: 16