当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - How do you replace the inverted commas for the following output and insert commas between the values as shown below using python regular expression

问题描述:

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.

网友答案:

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.

i.e. 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]
分享给朋友:
您可能感兴趣的文章:
随机阅读: