我有这个二进制数据列表:
binary:
[[1, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0]]
我正在尝试删除逗号和空格,以便得到 10 个二进制数,并且需要转换为十进制。
decimal=0
numbers=[]
for i in range(0,len(binary)):
numbers.append(map(float, str(binary[i]).split(',')))
#print(list(numbers))
decimal = decimal*2 + numbers[i] ---->this line give error #TypeError: unsupported operand type(s) for +: 'int' and 'map'
数字的输出是一张 map :
[<map at 0x7f8b62798080>,
<map at 0x7f8b62798208>,
<map at 0x7f8b627983c8>,
<map at 0x7f8b62798588>,
<map at 0x7f8b62798748>,..]
如何解决这个问题?或者有更好的方法吗?
最佳答案
一种更简单(因此更好)的方法是使用 int()
将二进制字符串转换为整数值:
>>> numbers = [int(''.join(str(bit) for bit in bits), 2) for bits in binary]
>>> numbers
[48, 16, 63, 16, 16, 16, 20, 20, 16, 20]
int() 的第二个参数指定传入数据的基数,基数 2 是二进制。 ''.join(str(bit) for bit in bits
将每个列表项的元素转换为字符串,然后将这些字符串连接起来以创建位字符串。
关于python - 如何在Python中从 map 对象(<map at 0x7f8adefd7c50>)获取二进制值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53061412/