I am using the following code below
y = bin(25)[2:]
y = [y]
tuple(y)
print 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.
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]
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.
ie 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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.