我在 Python 中有这样的列表:[1,0,0,0,0,0,0,0]
. 我可以像输入 0b10000000 那样将其转换为整数(即转换为 128)吗?我还需要将序列转换[1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0]
为整数(这里它将返回 0b1100000010000000,即 259)。如果需要,列表的长度始终是 8 的倍数。
问问题
29018 次
6 回答
58
您可以使用位移:
out = 0
for bit in bitlist:
out = (out << 1) | bit
这很容易击败 ARS 提出的“int cast”方法,或 Steven Rumbalski 提出的带有查找的修改转换:
>>> def intcaststr(bitlist):
... return int("".join(str(i) for i in bitlist), 2)
...
>>> def intcastlookup(bitlist):
... return int(''.join('01'[i] for i in bitlist), 2)
...
>>> def shifting(bitlist):
... out = 0
... for bit in bitlist:
... out = (out << 1) | bit
... return out
...
>>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import intcaststr as convert', number=100000)
0.5659139156341553
>>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import intcastlookup as convert', number=100000)
0.4642159938812256
>>> timeit.timeit('convert([1,0,0,0,0,0,0,0])', 'from __main__ import shifting as convert', number=100000)
0.1406559944152832
于 2012-09-17T14:25:16.543 回答
15
...或使用位串模块
>>> from bitstring import BitArray
>>> bitlist=[1,0,0,0,0,0,0,0]
>>> b = BitArray(bitlist)
>>> b.uint
128
于 2012-09-17T14:33:45.207 回答
7
试试这个单行:
int("".join(str(i) for i in my_list), 2)
如果您关心速度/效率,请查看 Martijn Pieters 的解决方案。
于 2012-09-17T14:25:39.750 回答
7
我遇到了一种稍微优于 Martijn Pieters 解决方案的方法,尽管他的解决方案当然更漂亮。我实际上对结果有点惊讶,但无论如何......
import timeit
bit_list = [1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0]
def mult_and_add(bit_list):
output = 0
for bit in bit_list:
output = output * 2 + bit
return output
def shifting(bitlist):
out = 0
for bit in bitlist:
out = (out << 1) | bit
return out
n = 1000000
t1 = timeit.timeit('convert(bit_list)', 'from __main__ import mult_and_add as convert, bit_list', number=n)
print "mult and add method time is : {} ".format(t1)
t2 = timeit.timeit('convert(bit_list)', 'from __main__ import shifting as convert, bit_list', number=n)
print "shifting method time is : {} ".format(t2)
结果:
mult and add method time is : 1.69138722958
shifting method time is : 1.94066818592
于 2012-09-17T21:18:24.627 回答
3
这个怎么样:
out = sum([b<<i for i, b in enumerate(my_list)])
或以相反的顺序:
out = sum([b<<i for i, b in enumerate(my_list[::-1])])
于 2019-04-18T19:36:45.100 回答
0
@Akavall 建议的最简单的方法是最快的方法。下面的 mult_add_xor 的附加时序表明,python 中的位操作较慢,因为简单的加法“+ bit”比 xor “| bit”快,并且乘以 2 比移位“<< 1”快。
import timeit
bit_list = [1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
def mult_and_add(bit_list):
output = 0
for bit in bit_list:
output = output * 2 + bit
return output
def mult_and_xor(bit_list):
output = 0
for bit in bit_list:
output = output * 2 | bit
return output
def shifting(bitlist):
out = 0
for bit in bitlist:
out = (out << 1) | bit
return out
n = 1000000
a1 = mult_and_add(bit_list)
a2 = mult_and_xor(bit_list)
a3 = shifting(bit_list)
print('a1: ', a1, ' a2: ', a2, ' a3: ', a3)
assert a1 == a2 == a3
t = timeit.timeit('convert(bit_list)',
'from __main__ import mult_and_add as convert, bit_list',
number=n)
print("mult and add method time is : {} ".format(t))
t = timeit.timeit('convert(bit_list)',
'from __main__ import mult_and_xor as convert, bit_list',
number=n)
print("mult and xor method time is : {} ".format(t))
t = timeit.timeit('convert(bit_list)',
'from __main__ import shifting as convert, bit_list',
number=n)
print("shifting method time is : {} ".format(t))
输出:
a1: 49280 a2: 49280 a3: 49280
mult and add method time is : 0.9469406669959426
mult and xor method time is : 1.0905388034880161
shifting method time is : 1.2844801126047969
于 2021-11-14T16:02:13.807 回答