假設,我需要替換檔案中的每個 's',前提是我知道其中有三個,并在每個串列元素發生的基礎上替換每個出現:
This is my test
串列:[1, 2, 3]
更換后所需的輸出:
Thi1 i2 my te3t
最優雅/pythonic的方法是什么?
uj5u.com熱心網友回復:
您可以將ss 替換為占位符并用于str.format填充:
s = "This is my test".replace('s','{}')
lst = [1, 2, 3]
out = s.format(*lst)
輸出:
'Thi1 i2 my te3t'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/430247.html
標籤:Python python-3.x 细绳 列表 代替
上一篇:僅選擇物件內的特定鍵值對
