Python:如果字符串包含特定关键字,则从列表中删除特定字符串

Python:如果字符串包含特定关键字,则从列表中删除特定字符串,python,string,list,filter,Python,String,List,Filter,如果字符串包含某些单词,我试图排除字符串列表中的某些字符串 例如,如果字符串中有一个单词,“肉桂”或“水果”或“吃”,我希望将其从字符串列表中排除 ['RT @haussera: Access to Apple Pay customer data, no, but another way? everybody wins - MarketWatch http://t.co/Fm3LE2iTkY', "Landed in the US, tired w horrible migrane. The o

如果字符串包含某些单词,我试图排除字符串列表中的某些字符串

例如,如果字符串中有一个单词,“肉桂”或“水果”或“吃”,我希望将其从字符串列表中排除

['RT @haussera: Access to Apple Pay customer data, no, but another way? everybody wins - MarketWatch http://t.co/Fm3LE2iTkY', "Landed in the US, tired w horrible migrane. The only thing helping- Connie's new song on repeat. #SoGood #Nashville https://t.co/AscR4VUkMP", 'I wish jacob would be my cinnamon apple', "I've collected 9,112 gold coins! http://t.co/T62o8NoP09 #iphone, #iphonegames, #gameinsight", 'HAHAHA THEY USED THE SAME ARTICLE AS INDEPENDENT http://t.co/mC7nfnhqSw', '@hot1079atl Let me know what you think of the new single "Mirage "\nhttps://t.co/k8DJ7oxkyg', 'RT @SWNProductions: Hey All so we have a new iTunes listing due to our old one getting messed up please resubscribe via the following https…', 'Shawty go them apple bottoms jeans and the boots with the furrrr with furrrr the whole club is looking at herYou can use 
any
in a list comprehension to filter for you

original_list = ['RT @haussera: Access to Apple Pay customer data, no, but another way? everybody wins - MarketWatch http://t.co/Fm3LE2iTkY', "Landed in the US, tired w horrible migrane. The only thing helping- Connie's new song on repeat. #SoGood #Nashville https://t.co/AscR4VUkMP", 'I wish jacob would be my cinnamon apple', "I've collected 9,112 gold coins! http://t.co/T62o8NoP09 #iphone, #iphonegames, #gameinsight", 'HAHAHA THEY USED THE SAME ARTICLE AS INDEPENDENT http://t.co/mC7nfnhqSw', '@hot1079atl Let me know what you think of the new single "Mirage "\nhttps://t.co/k8DJ7oxkyg', 'RT @SWNProductions: Hey All so we have a new iTunes listing due to our old one getting messed up please resubscribe via the following https…', 'Shawty go them apple bottoms jeans and the boots with the furrrr with furrrr the whole club is looking at herOne solution is the following:

list = [sent for sent in list 
    if not any(word in sent for word in keywordFilter)]

['RT@haussera:访问Apple Pay客户数据,不是,而是另一种方式?每个人都赢-MarketWatchhttp://t.co/Fm3LE2iTkY“登陆美国,疲倦和可怕的偏头痛。唯一有帮助的是——康妮的新歌《重播》。"索古德"纳什维尔https://t.co/AscR4VUkMP“,‘我希望雅各布是我的桂皮苹果’,“我收集了9112枚金币!http://t.co/T62o8NoP09 #iphone、#iphonegames、#gameinsight、#HAHAHA他们使用了与《独立报》相同的文章http://t.co/mC7nfnhqSw“,”@hot1079atl让我知道你对新单曲《海市蜃楼》的看法"\nhttps://t.co/k8DJ7oxkyg“,”RT@SWN产品:嘿,大家好,我们有一个新的iTunes列表,因为我们的旧列表被弄乱了,请通过以下https重新订阅…,“Shawty go他们的苹果底牛仔裤和靴子搭配毛茸茸的毛茸茸的毛茸茸的整个俱乐部都在看着她你可以在列表中使用
any
进行筛选给你

list = [sent for sent in list 
    if not any(word in sent.split(' ') for word in keywordFilter)]

original_list=['RT@haussera:访问Apple Pay客户数据,不是,而是另一种方式?每个人都赢-MarketWatchhttp://t.co/Fm3LE2iTkY“登陆美国,疲倦和可怕的偏头痛。唯一有帮助的是——康妮的新歌《重播》。"索古德"纳什维尔https://t.co/AscR4VUkMP“,‘我希望雅各布是我的桂皮苹果’,“我收集了9112枚金币!http://t.co/T62o8NoP09 #iphone、#iphonegames、#gameinsight、#HAHAHA他们使用了与《独立报》相同的文章http://t.co/mC7nfnhqSw“,”@hot1079atl让我知道你对新单曲《海市蜃楼》的看法"\nhttps://t.co/k8DJ7oxkyg“,”RT@SWN产品:嘿,大家好,我们有了一个新的iTunes列表,因为我们的旧列表变得一团糟,请通过以下https重新订阅…“,”Shawty go That the apple Bottom jeans and the boots with the Furr with the Furrr整个俱乐部都在关注她一个解决方案是:

final_list = []
for i in original_list:
    temp = []
    for k in i.split(" "):
        if not any(i for i in keywordFilter if i in k):
            temp.append(k)
    final_list.append(" ".join(temp))
print final_list
它将删除包含列表
keywordFilter
中的一个单词作为子字符串的所有字符串。 例如,它将删除第二个字符串,因为它包含单词
repeat
(而
eat
repeat
的子字符串)

如果要避免这种情况,可以执行以下操作:

final_list = []
for i in original_list:
    temp = []
    for k in i.split(" "):
        if not any(i for i in keywordFilter if i in k):
            temp.append(k)
    final_list.append(" ".join(temp))
print final_list
它将只删除包含列表中一个单词的字符串作为子单词(即,由句子中的空格分隔)。

简单复杂:)


堆栈溢出认为我是机器人哈哈哈哈哈你只是想从字符串中删除该关键字?或者如果字符串有关键字,则删除整个字符串?@Cyber有关键字的字符串。@Cyber我有一堆字符串,我希望排除那些有我要排除的某些关键字的字符串。@ArnoldChung你应该避免命名你的v变量“list”,因为这是一个函数。此外,您应该避免在迭代列表时修改列表。请参阅。感谢您的代码。我有一个简单的问题要问。在python3中,您的代码完美地从字符串中排除了关键字。但是,在终端上运行代码不起作用。换句话说,假设我在test.py上保存了此代码。我启动了t他在终端上输入“python3test.py”并运行它。它返回我[],空字符串。你知道为什么吗?