![](/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.