I am using the following code below
y = bin(25)[2:]
y = [y]
The output here is ['11001'] and I am looking for the output [1,1,0,0,1] using regular expression so as to automate the plotting a square wave for the generated binary values from decimal numbers.Could any help me out.
from the python docs:
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer.
y is a string.
If you want to convert it to an array of integers, you need to actualyl do that conversion yourself:
y = bin(25)[2:] y = [int(yi) for yi in y] print y # [1, 1, 0, 0, 1]
You dont need regex for such problems you can use
map to map the
int function on each element in your list :
>>> y = bin(25)[2:] >>> y '11001' >>> map(int,y) [1, 1, 0, 0, 1]
or just use a list comprehension :
>>> [int(i) for i in y] [1, 1, 0, 0, 1]