[英]Remove quotes around strings
array = format([
v if v is not None else "*" for v in self._tree.bfs_order_star()
])
上面的代碼以以下格式返回一個字符串:
輸出: [ 10, 5, 15, '*', '*','*', 20 ]
如何更改它以使 *(stars)、非值不被引號包圍? 我嘗試了以下但沒有成功。
array = format([
v if v is not None else "*" for v in self._tree.bfs_order_star()
]).strip('"\'')
打印時使用replace()
方法刪除引號:
#Convert list to string and replace/remove specific characters.
str(lst).replace("[character to replace/remove]", "[character to replace with or leave empty to remove.]")
您可以創建一個類/對象,其表示實際上只是一顆星:
class Star:
def __repr__(self):
return '*'
一個演示:
>>> print([1, '*', 2])
[1, '*', 2]
>>> print([1, Star(), 2])
[1, *, 2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.