如何循环浏览列表并取出几个不同的变量



如何循环遍历字符串列表并提取出三个不同的变量?我试图解决的问题是课程的一部分,但没有给出解决方案。

问题:如何循环浏览一组推文并取出三个不同的变量:"happy_tweets"sad_ weets";,以及";neutral_tweets";

我很难找到循环浏览推文并提取出每种推文的最佳方式。

tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python. #zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
happy_tweets = 0
sad_tweets = 0
neutral_tweets = 0

for tweet in tweets:
if happy_words in tweets:
happy_tweets += 1
print(happy_tweets)

我建议使用类似于列表综合的集合综合生成每组tweet的新列表。然后,你可以将原始推文列表与快乐和悲伤推文集的并集进行比较。像这样:

happy_tweets = {t for t in tweets for w in happy_words if w in t}
sad_tweets = {t for t in tweets for w in sad_words if w in t}
neutral_tweets = set(tweets) - (happy_tweets | sad_tweets)
print(list(happy_tweets))
print(list(sad_tweets))
print(list(neutral_tweets))

哪个给出:

['Apple announces the release of the new iPhone 12. Fans are excited.', "I'm really excited to learn Python with @JovianML #zerotopandas", 'Spent my day with family!! #happy', 'Freecodecamp has great coding tutorials. #skillup', 'Wow, what a great day today!! #sunshine', 'This is a really nice song. #linkinpark']
['I feel sad about the things going on around us. #covid19', 'Why do bad things happen to me?']
['Check out my blog post on common string operations in Python. #zerotopandas', 'The python programming language is useful for data science']

在这里使用集合而不是仅在happy_tweetssad_tweets中使用列表的原因是,在列表中有多个单词与tweet字符串匹配的情况下,防止重复。一个稍微更有效的方法,尽管更详细,是将这些语句移到实际的for语句中,并在第一个单词匹配时中断。但这并不是时间复杂性的重大变化。


顺便说一句,联合运算必须分组的原因是基于运算符优先级

这是我的代码,可能更长,但我只是想用另一种方式向您展示解决方案。过滤掉所有快乐和悲伤的推文,剩下的每一条都是中性的。使用len((函数按推文的种类计算推文的数量

tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python. #zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
happy_tweets_list = []
sad_tweets_list = []
neutral_tweets_list = [item for item in tweets]

for tweet in tweets:
for word in happy_words:
if word in tweet:
happy_tweets_list.append(tweet)
neutral_tweets_list.remove(tweet)
for word in sad_words:
if word in tweet:
sad_tweets_list.append(tweet)
neutral_tweets_list.remove(tweet)
happy_tweets = len(happy_tweets_list)
sad_tweets = len(sad_tweets_list)
neutral_tweets = len(neutral_tweets_list)

另一种查找子字符串是否在字符串列表中的方法。

tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python. #zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
happy_tweets = 0
sad_tweets = 0
neutral_tweets = 0

for tweet in tweets:
if any(happy_word in tweet for happy_word in happy_words):
happy_tweets += 1
elif any(sad_word in tweet for sad_word in sad_words):
sad_tweets += 1
else:
neutral_tweets += 1
print(happy_tweets)
print(sad_tweets)
print(neutral_tweets)

输出

6
2
2

最新更新