我有一個清單 am = ['a:hull', 'b:2', 'c:3']
我想am = ['a:london', 'b:2', 'c:3']通過使用函式將船體更改為倫敦,而不更改其余部分。
我試過了am = {a:london},但它改變了整個串列,我想保持其他資訊原樣
任何幫助將非常感激
uj5u.com熱心網友回復:
您可以使用索引訪問串列項,更改項的方式相同。如果您知道串列中專案的索引(位置),那么您可以執行以下操作:
am[0] = "a:london"
其中 0 是專案的索引。
uj5u.com熱心網友回復:
這是您可以使用的快速功能:
am = ['a:hull', 'b:2', 'c:3']
def replace(lst):
lst[0] = "a:london"
print(lst)
replace(am)
我們只是將串列中的第一個字串替換為"a:london",然后將其列印出來。
uj5u.com熱心網友回復:
你可以在python中使用內置的replace()函式。
strings = ['a:hull', 'b:2', 'c:3']
new_strings = []
for string in strings:
new_string = string.replace("a:hull","a:london")
new_strings.append(new_string)
print(new_strings)
輸出將是 ['a:london', 'b:2', 'c:3']
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/327268.html
