如何检查列表中哪些单词包含在字符串中



我想从python中的字符串中包含的列表中收集每个单词。我找到了一些解决方案,但到目前为止我得到了:

data = "Today I gave my dog some carrots to eat in the car"
tweet = data.lower()                             #convert to lower case
split = tweet.split()
matchers = ['dog','car','sushi']
matching = [s for s in split if any(xs in s for xs in matchers)]
print(matching)

结果是

['dog', 'carrots', 'car']

我该如何解决结果是只有狗和车,而没有给我的火柴添加空间的问题?

此外,我如何从数据字符串中删除任何$符号(例如(,但不删除其他特殊字符(如@(?

How do I fix that the result is only dog and car without adding spaces to my matchers?

要使用当前代码执行此操作,请替换此行:

matching = [s for s in split if any(xs in s for xs in matchers)]

有了这个:

matching = []
# iterate over all matcher words
for word in matchers:
if word in split:  # check if word is in the split up words
matching.append(word)  # add word to list

你还提到了这个:

Also how would I remove any $ signs (as example) from the data string but no other special characters like @?

要做到这一点,我会创建一个列表,其中包含你想要删除的字符,比如:

things_to_remove = ['$', '*', '#']  # this can be anything you want to take out

然后,在拆分之前,只需从推特字符串中剥离每个字符

for remove_me in things_to_remove:
tweet = tweet.replace(remove_me, "")

最后一个代码块演示了所有这些主题:

data = "Today I@@ gave my dog## some carrots to eat in the$ car"
tweet = data.lower()                             #convert to lower case
things_to_remove = ['$', '*', '#']
for remove_me in things_to_remove:
tweet = tweet.replace(remove_me, "")
print("After removeing characters I don't want:")
print(tweet)
split = tweet.split()
matchers = ['dog','car','sushi']
matching = []
# iterate over all matcher words
for word in matchers:
if word in split:  # check if word is in the split up words
matching.append(word)  # add word to list
print(matching)

最新更新