简体   繁体   English

二进制列表为1和0到int的列表

[英]Binary as list of 1s and 0s to int

I would like to convert a list of integer 1s and 0s which represent a binary number to an int. 我想将一个表示二进制数的整数1和0的列表转换为int。

something along the lines of: 类似的东西:

>>> [1,1,0,1].toint()

would give an output of 13 会产生13的输出

Strings are unnecessary here: 这里不需要字符串:

>>> l = [1,1,0,1]
>>> 
>>> sum(j<<i for i,j in enumerate(reversed(l)))
13

Relevant documentation: 相关文件:

You can do: 你可以做:

>>> int(''.join(map(str, my_list)), 2)
5

Look at this: 看这个:

>>> x = [1,1,0,1]
>>> int("".join(map(str, x)), 2)
13
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM