简体   繁体   English

在句子中找到一个单词并将整个句子替换为一个数字

[英]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.

相关问题 用列表中的单词替换句子中的单词并复制列中的新句子 - Replace a word in a sentence with words from a list and copying the new sentences in a column 在Python中查找特定单词的句子索引(列表中的句子) - Find the sentence’s index (sentences in a list) of a specific word in Python 使用字典查找句子中的字符串并将其替换为句子列表中的另一个字符串 - Find and replace a string in a sentence to another string in a list of sentences using a dictionary Python:用单词列表替换句子中的一个单词,并将新句子放在 pandas 的另一列中 - Python: Replace one word in a sentence with a list of words and put thenew sentences in another column in pandas 在句子列表中查找单词前数字的平均值 - Find average value of number before word in a list of sentences 找到并用正确的句子替换以小写字母开头的句子。 正则表达式或崇高 - find and replace with correct sentence case sentences starting with lowercase. regex or sublime 在另一个列表的句子中查找列表中的单词,并在Python 2.7中将其替换 - Find a word of a list in a sentence of another list and replace it in Python 2.7 如何使用find function来帮助替换句子中的单词? - How to use the find function to help replace a word in a sentence? 用“相同”一词替换重复的句子 - replace the duplicated sentences with word "same" 将熊猫中的句子分成句子数和单词 - Split sentences in pandas into sentence number and words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM