Python从数字组中获取数字

sno*_*ake 3 python numbers

我想知道我可以用一组数字建立一个数字的次数:

possible_numbers = 1, 2, 4, 8, 16
Run Code Online (Sandbox Code Playgroud)

如果我想要23号我需要

1x 16
0x 8
1x 4
1x 2
1x 1 
Run Code Online (Sandbox Code Playgroud)

Python中是否有内置函数来执行此操作?

编辑:数字固定为1,2,4,8,16,32,64,128.可以进行多种选择.

由于没有功能构建,我会自己编写代码.

Jun*_*uxx 11

假设可能的数字总是2的幂,你基本上想要将数字转换为二进制格式.使用内置bin功能很容易:

>>> mylist = [int(x) for x in bin(23)[2:]]
>>> print mylist
[1, 0, 1, 1, 1]
Run Code Online (Sandbox Code Playgroud)

要获得与您在问题中显示的输出完全相同的输出:

>>> for i, j in enumerate(mylist):
...     print '%ix %i' % (j, 2**(len(mylist)-i-1))
...
1x 16
0x 8
1x 4
1x 2
1x 1
Run Code Online (Sandbox Code Playgroud)