小编Sub*_*due的帖子

如何将整数转换为Python中的位列表

我想知道是否有比现有方法更好的方法.

我试图将一个整数表示为一个位列表,只有当整数<128时才将其填充为8位:

Example input: 0x15
Desired output: [0, 0, 0, 1, 0, 1, 0, 1]
Run Code Online (Sandbox Code Playgroud)

我是通过以下方式完成的:

input = 0x15
output = deque([int(i) for i in list(bin(input))[2:]])
while len(output) != 8:
    output.appendleft(0)
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法在python中执行此操作?

编辑: 我想将任何整数转换为二进制列表.仅当数字需要少于8位来表示时才填充到8.

Another Example input: 0x715
Desired output: [1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1]
Run Code Online (Sandbox Code Playgroud)

python string list

7
推荐指数
4
解决办法
4340
查看次数

标签 统计

list ×1

python ×1

string ×1