python - 如何在Python中从 map 对象(<map at 0x7f8adefd7c50>)获取二进制值?

标签 python python-3.x

我有这个二进制数据列表:

 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/

相关文章:

Python: Pandas 数据框:删除 " "BOM 字符

Python 3 从互联网广播流中获取歌曲名称

python - 从python中的.txt文件中读取特殊字符

python - NumPy 中 random_integers 的替代方案

Python,matplotlib - 基于条件变量的图例 -

python - 有没有办法计算 XGBoost 树中每个节点的值?

python-3.x - 根据现有列中的值创建新列

python - 为什么屏幕无法以 Kv 语言加载?

python - 如何在 PyGame 中使用 alpha channel 渲染透明文本?

Python 的多处理 map_async 在 Windows 上生成错误