I have the following scenario:
x = 0b0111
I would like to convert this value to:
y = [0, 1, 1, 1]
When I convert x = 0b1001, I can get y = [1, 0, 0, 1], but when I try to do the same for x = 0b0111, and then convert back with str(bin(y)) - I seem to lose the leading 0, and get 0b111.
Any suggestions?
解决方案
Once you get that string 0b111, it's straightforward to split out the digits that you're interested in. For each character of everything after the 0b in the string, convert it to an integer.
[int(d) for d in str(bin(x))[2:]]