我正在嘗試將后綴添加到現有的array. 下面是我的代碼
print('a' [10, 100])
有了這個我得到以下錯誤
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str
你能幫鋤頭做那件事嗎?我可以使用一些for回圈,但我相信可能有更直接的方法來實作相同的目標。
uj5u.com熱心網友回復:
您可以創建一個新的串聯陣列:
>>> ['{0}{1}'.format('a', num) for num in [10, 100]]
['a10', 'a100']
從檔案中讀取字串格式和串列理解。
uj5u.com熱心網友回復:
如果我理解您的問題,您需要一個新的字串陣列(串列)。你可以試試這個:
new_lst = ['a' str(x) for x in [10, 100]] # just use string concatentation
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412100.html
標籤:
