如果我有二进制列表
a=['0b11001000', '0b01001100', '0b00111100', '0b00011111',
'0b11110000', '0b01011010', '0b10010110', '0b00011110']
我想将所有字符串元素转换为整数,并且这次将它很好地放回到二进制列表中:
a=[0b11001000, 0b01001100, 0b00111100, 0b00011111,
0b11110000, 0b01011010, 0b10010110, 0b00011110]
我该怎么办?
答案 0 :(得分:6)
将int
与基础2:
>>> a=['0b11001000', '0b01001100', '0b00111100', '0b00011111',
... '0b11110000', '0b01011010', '0b10010110', '0b00011110']
>>> [int(x, 2) for x in a]
[200, 76, 60, 31, 240, 90, 150, 30]
答案 1 :(得分:5)
尝试一下:
a = [int(x, 2) for x in a]
答案 2 :(得分:4)
虽然在第二个列表中,您将整数表示为二进制,但它们仍然是int
类型。但是,您可以转换字符串:
a = [int(x, 2) for x in a]