Python 标识列表项是否在字符串中
我试图创建一个嵌套的循环序列,它查看一系列停止字和一系列字符串,并确定每个停止字是否在每个列表项中。理想情况下,我希望能够将每个字符串中出现的单词添加到一个新列中,并将它们全部从字符串中删除 有人有小费吗?我的循环顺序是否错误Python 标识列表项是否在字符串中,python,loops,for-loop,Python,Loops,For Loop,我试图创建一个嵌套的循环序列,它查看一系列停止字和一系列字符串,并确定每个停止字是否在每个列表项中。理想情况下,我希望能够将每个字符串中出现的单词添加到一个新列中,并将它们全部从字符串中删除 有人有小费吗?我的循环顺序是否错误 def remove_stops(text, customStops): """ Removes custom stopwords. Parameters ---------- text : the
def remove_stops(text, customStops):
"""
Removes custom stopwords.
Parameters
----------
text : the variable storing strings from which
stopwords should be removed. This can be a string
or a pandas DataFrame.
customStops : the list of stopwords which should be removed.
Returns
-------
Cleansed lists.
"""
for item in text:
print("Text:", item)
for word in customStops:
print("Custom Stops: ", word)
if word in item:
print("Word: ", word)
#Add word to list of words in item
#Remove word from item
以下是您可以做的:
def remove_stops(text, customStops):
found = {k:[] for k in text} # Dict for all found stopwords in text
for i,item in enumerate(text):
for word in customStops:
text[i] = text[i].replace(word,'') # Remove all stopwords from each string, if the stopword is not in, the replace will just leave it as it is
if word in item:
found[item].append(word)
return text, found
text = ['Today is my lucky day!',
'Tomorrow is rainy',
'Please help!',
'I want to fly']
customStops = ['help', 'fly']
clean, found = remove_stops(text, customStops)
print(clean)
print(found)
输出:
['Today is my lucky day!',
'Tomorrow is rainy',
'Please !',
'I want to ']
{'Today is my lucky day!': [],
'Tomorrow is rainy': [],
'Please help!': ['help'],
'I want to fly': ['fly']}
如果您提供一些测试输入以及对remove_stops()函数的调用,我相信会有人(比如我;-)可以帮助您。“转到新专栏”是什么意思?@Ronald感谢您添加此请求,这是我在SO上的第一篇帖子:)我以后一定会这么做!非常感谢你,安!你找到你想要的了吗?我有一个澄清的问题-将两个列表转换为字典的原因是为了加快迭代,还是为了确保停止词与初始文本正确匹配?当我尝试运行此操作时,我得到错误“Unhable type:'list'”,因为它试图从列表值创建字典键。有办法解决这个问题吗?谢谢安!显然,我试图使用列表列表创建键,但这是不可能的,所以我将列表展平并转换为字符串。