[英]find a word in the sentences and replace whole sentence with a number
I'm a beginner.我是初学者。 I have 757 rows with a different statement.
我有 757 行不同的语句。 I would like to write a command to find the statement with "e-bike" and replace it with the number 1: 1st statement : "a conflict between cyclists and pedestrians, a conflict between two cyclists, a conflict between e-biker and a cyclist" 2nd statement: "a conflict relating to a dog(s), a conflict between cyclists and pedestrians, a conflict between an e-biker and a pedestrian, a conflict between e-biker and a cyclist" If I want to replace one by one I have to write the 757 command.
我想编写一个命令来查找带有“e-bike”的语句并将其替换为数字 1:第一个语句:“骑自行车的人和行人之间的冲突,两个骑自行车的人之间的冲突,电动自行车和一辆骑自行车的人”第二个陈述: “与狗有关的冲突,骑自行车的人和行人之间的冲突,电动自行车和行人之间的冲突,电动自行车和骑自行车的人之间的冲突”如果我想更换一个通过一个我必须编写 757 命令。
Thanks谢谢
Elham艾勒姆
You can use .replace('<old_string>', '<new_string>')
while iterating through your rows.您可以在遍历行时使用
.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
Or even with list comprehension:甚至使用列表理解:
def replace_rows(rows):
return [i.replace('e-bike', '1') for i in rows]
You may try something like, assuming you have a list of sentences sentences_list
:您可以尝试类似的方法,假设您有一个句子列表
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.