【发布时间】:2020-08-10 11:11:20
【问题描述】:
在我的列表理解中,如果"RT @tiktoksaudi2:"occurrences 存在于列表中,我会尝试删除它,但我得到一个空列表,即使它不存在于列表中
test=["The way to a man's heart is through his stomach. - Sarah Willis Parton", 'A first rate soup is better than a second rate painting. - Abraham Maslow', 'A good cook is like a sorceress who dispenses happiness. - Elsa Schiaparelli', "A man's palate can, in time, become accustomed to anything. - Napoleon Bonaparte", 'Savory seasonings stimulate the appetite. - Latin Proverb', 'Cooking is an observation-based process that you can’t do if you’re so completely focused on a recipe. - Alton Brown', 'Happiness is finding three olives in your martini when you’re hungry. - Johnny Carson', 'A good meal makes a man feel more charitable toward the world than any sermon. - Arthur Pendenys', 'Wine and cheese are ageless companions, like aspirin and aches, or June and moon, or good people and noble ventures. - M. F. K. Fisher', 'For hunger is a sauce, well blended and prepared, for any food. - Chrétien de Troyes', "Without my morning coffee I'm just like a dried up piece of roast goat. - Johann Sebastian Bach", 'You know how I feel about tacos. It’s the only food shaped like a smile. A beef smile. - Earl Hickey']
print(test)
sh3rtext=[text.replace("RT @tiktoksaudi2:","") for text in test if "RT @tiktoksaudi2:" in text]
print(sh3rtext)
【问题讨论】:
-
你得到一个空列表因为它在列表中不存在 - 你只在输出中包含确实包含它的项目。这种情况真的没有意义,因为 replace 不会更改不包含它的字符串。
标签: python python-3.x list higher-order-functions