小编ort*_*ng2的帖子

删除列表单词组合python 3

我有一个字符串列表,我想搜索一个单词组合.如果组合不存在,则删除列表.是否有一个python列表理解可行?

word_list = ["Dogs love ice cream", "Cats love balls", "Ice cream", "ice cream is good with pizza", "cats hate ice cream"]

keep_words = ["Dogs", "Cats"] 

Delete_word = ["ice cream"]
Run Code Online (Sandbox Code Playgroud)

删除里面有冰淇淋的单词,但如果句子中有狗或猫,请保留它.

Desired_output = ["Dogs love ice cream", "Cats love balls", "cats hate ice cream"] 
Run Code Online (Sandbox Code Playgroud)

尝试此代码也试过AND和OR,但无法正确组合.

output_list = [x for x in  word_list if "ice cream" not in x]
Run Code Online (Sandbox Code Playgroud)

python arrays for-loop list

5
推荐指数
2
解决办法
111
查看次数

如何从列表python中删除一个字符串

是否可以删除字符串,只有列表

data = [
"50,bird,corn,105.4,"
"75,cat,meat,10.3,"
"100,dog,eggs,1000.5,"
]
Run Code Online (Sandbox Code Playgroud)

希望它看起来像这样

data = [
50,'bird','corn',105.4,
75,'cat','meat',10.3,
100,'dog','eggs',1000.5,
]
Run Code Online (Sandbox Code Playgroud)

python string list

-6
推荐指数
1
解决办法
69
查看次数

标签 统计

list ×2

python ×2

arrays ×1

for-loop ×1

string ×1