簡體   English   中英

如何使用以下正則表達式替換以下輸出的反逗號並在值之間插入逗號,如下所示

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

我在下面使用以下代碼

y = bin(25)[2:]
y = [y]
tuple(y)
print y

這里的輸出是['11001'],我正在使用正則表達式查找輸出[1,1,0,0,1],以便自動繪制從十進制數生成的二進制值的方波。幫幫我。

您不需要正則表達式來解決此類問題,可以使用map將int函數map到列表中的每個元素上:

>>> y = bin(25)[2:]
>>> y
'11001'
>>> map(int,y)
[1, 1, 0, 0, 1]

或只使用列表理解:

>>> [int(i) for i in y]
[1, 1, 0, 0, 1]

來自python docs

將整數轉換為二進制字符串。 結果是有效的Python表達式。 如果x不是Python int對象,則它必須定義一個返回整數的index ()方法。

y是一個字符串。

如果要將其轉換為整數數組,則需要自己實際進行轉換:

y = bin(25)[2:]
y = [int(yi) for yi in y]
print y # [1, 1, 0, 0, 1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM