使用列表理解后的空列表
empty list after using list comprehension
在我的列表理解中,我试图删除"RT @tiktoksaudi2:"
出现在列表中的事件,但我得到一个空列表,即使它不存在于列表中
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)
列表理解中的if
子句过滤掉任何不符合条件的项目; IE。在您的示例中,任何不包含“RT @tiktoksaudi2:”的项目(-> 全部)。只需省略 if "RT @tiktoksaudi2:" in text
并对所有元素执行替换调用(如果元素不包含您的字符串而只包含 return 原始元素,这将不执行任何操作)以取回整个列表。
在我的列表理解中,我试图删除"RT @tiktoksaudi2:"
出现在列表中的事件,但我得到一个空列表,即使它不存在于列表中
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)
列表理解中的if
子句过滤掉任何不符合条件的项目; IE。在您的示例中,任何不包含“RT @tiktoksaudi2:”的项目(-> 全部)。只需省略 if "RT @tiktoksaudi2:" in text
并对所有元素执行替换调用(如果元素不包含您的字符串而只包含 return 原始元素,这将不执行任何操作)以取回整个列表。