Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将表示二进制数的整数 1 和 0 列表转换为 int。
类似于:
>>> [1,1,0,1].toint()
会给出一个输出13
13
这里不需要字符串:
>>> l = [1,1,0,1] >>> >>> sum(j<<i for i,j in enumerate(reversed(l))) 13
相关文件:
sum()
enumerate()
reversed()
你可以做:
>>> int(''.join(map(str, my_list)), 2) 5
看这个:
>>> x = [1,1,0,1] >>> int("".join(map(str, x)), 2) 13 >>>