![](/img/trans.png)
[英]Replace a word in a sentence with words from a list and copying the new sentences in a column
[英]find a word in the sentences and replace whole sentence with a number
我是初學者。 我有 757 行不同的語句。 我想編寫一個命令來查找帶有“e-bike”的語句並將其替換為數字 1:第一個語句:“騎自行車的人和行人之間的沖突,兩個騎自行車的人之間的沖突,電動自行車和一輛騎自行車的人”第二個陳述: “與狗有關的沖突,騎自行車的人和行人之間的沖突,電動自行車和行人之間的沖突,電動自行車和騎自行車的人之間的沖突”如果我想更換一個通過一個我必須編寫 757 命令。
謝謝
艾勒姆
您可以在遍歷行時使用.replace('<old_string>', '<new_string>')
。
def replace_rows(rows)
for i in range(len(rows)):
rows[i] = rows[i].replace('e-bike', '1')
return rows
甚至使用列表理解:
def replace_rows(rows):
return [i.replace('e-bike', '1') for i in rows]
您可以嘗試類似的方法,假設您有一個句子列表sentences_list
:
results = []
for sentence in sentences_list:
results.append(sentence.replace("e-bike", "1"))
print(results)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.