如何比较两个列表?

How to compare two lists?

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']

问题:确定数据集中可以归类为快乐的推文数量。

我的代码:

number_of_happy_tweets = 0
 
for i in tweets:
  for x in i:
    if x in happy_words:
      number_of_happy_tweets += len(x)

为什么这段代码不起作用??????

您正在迭代推文中的字母并检查该字母是否在 happy_words 中,您需要做的是:

for tweet in tweets:
 number_of_happy_tweets += any(word in tweet for word in happy_words)

这意味着只要在推文中发现任何快乐的词,您就会将 number_of_happy_tweets 加一。

问题出在你的代码中。

你的第一行代码没问题for i in tweets

但是在第二行,你使用

for i in tweets:
    for x in i: # Try to print `x`
        print(x) #
W
o
w
,
 
w
h
a
t
 
a
 

.
.
.

Here you got the letter from tweets.

After that, you try to check these letters in your happy_words list.

试试这个代码。

happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
number_of_happy_tweets = 0

number_of_happy_tweets = 0
 
for i in tweets:
  for x in happy_words:
    if x in i:
        number_of_happy_tweets+=1
        break

形成术语的正则表达式交替,然后使用列表理解和 re.search:

happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
regex = r'(?:' + r'|'.join(happy_words) + r')'
num_tweets = len([x for x in tweets if re.search(regex, x)])
print(num_tweets)  # 6

嗨,在第二个循环中,您正在遍历元素(字母),而不是单词。迭代单词使用 split() 如下所示还要注意 number_of_happy_tweets 每次增加 1 而不是长度:

for i in tweets:
    for x in i.split():
        if x in happy_words:
            number_of_happy_tweets += 1

但请注意,如果在一条推文中您有两个(或更多)快乐词,代码会将其计为两个,或者即使一个快乐词与其他符号(如#)结合使用也不会以这种方式计算,所以我建议使用以下代码:

for tweet in tweets:
    if any(happy_word in tweet for happy_word in happy_words):
        number_of_happy_tweets += 1