简体   繁体   English

如果字符串包含特定字符,如何从字符串中删除单词?

[英]How to remove word from a string if it contains a specific character?

Ok, lets say my string contains the following: "$$$FOOD WATERMELON" .好的,假设我的字符串包含以下内容: "$$$FOOD WATERMELON" How would I remove the "$$$FOOD" from the string?如何从字符串中删除"$$$FOOD"

And lets say I have strings in a list:假设我在列表中有字符串:

data_list = ["$$$FOOD WATERMELON", "$$$STORY I walked to the local store"]

The method that I have in my code splits the elements in the list, then iterates through the lists inside of the list and removes any element that contains "$$$" which would work fine, if it weren't for the fact that the .split() function splits every word, so the list would end up looking like this: [["WATERMELON"],["I","walked","to","the","local,"store"]] which is not optimal, because then I would have to join the elements in the lists of the list, that takes more time.我在代码中的方法拆分列表中的元素,然后遍历列表内的列表并删除任何包含"$$$"元素,如果不是因为.split()函数拆分每个单词,因此列表最终将如下所示: [["WATERMELON"],["I","walked","to","the","local,"store"]]这不是最优的,因为那样我将不得不加入列表中的元素,这需要更多的时间。

Basically, the only thing I am wondering is: how do I remove a word in a string if it contains "$$$" .基本上,我唯一想知道的是:如果字符串中包含"$$$"我该如何删除它。 So this string: word = "$$$STORY I walked to the store" would become "I walked to the store"所以这个字符串: word = "$$$STORY I walked to the store"将变成"I walked to the store"

Sorry if this was confusing对不起,如果这令人困惑

You can try this:你可以试试这个:

>>> word = "$$$STORY I walked to the store"
>>> if word.startswith('$',0,word.index(' ')):
        word=word[word.index(' ')+1:]

>>> word
'I walked to the store'

Here is how it looks using the list and the function:以下是使用列表和函数的外观:


def check_word(word):
    if word.startswith('$',0,word.index(' ')):
        return word[word.index(' ')+1:]
data_list = ["$$$FOOD WATERMELON", "$$$STORY I walked to the local store"]
list2=[check_word(x) for x in data_list]
print(list2)

Try this one.试试这个。

import re

data_list = ["$$$FOOD WATERMELON", "$$$STORY I walked to the local store"]
output = [re.sub(r'\s*[$]+\w+\s*', '', x) for x in data_list]

This gave me the following output:这给了我以下输出:

['WATERMELON', 'I walked to the local store']

Here's regex definition:这是正则表达式定义:

\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [$]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ matches the character $ with index 3610 (2416 or 448) literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)

This code checks and ensures there is no substring at any word, not just the first word like some of the other answers.此代码检查并确保任何单词都没有子字符串,而不仅仅是像其他一些答案一样的第一个单词。

Output:输出:

['WATERMELON', 'I walked to the local store']

Code:代码:

data_list = ["$$$FOOD WATERMELON", "$$$STORY I walked to the local store"]

for word in range(len(data_list)):
  temp1 = data_list[word].split(" ")
  finished = False
  while not (finished):
      for temp2 in range(len(temp1)):
          if "$$$" in temp1[temp2]:
              temp1.pop(temp2)
              break
          finished = True
  data_list[word] = ' '.join(temp1)

print(data_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM