为了我自己的理解,我编写了2个函数并使用我的例子进行测试。同样,两个函数都有1个示例右和1个错误。 1。 def count_from_word_list(tweet,L): “”(str,str of list) - > int
第一个参数代表推文。第二个参数是单词列表。计算列表中任何单词出现的次数,并返回总数。
>>> count_from_word_list('I like him and I like her', ["like","her"])
3
>>> count_from_word_list('I like him and he likes me', ["like","her"])
1
"""
count = 0
for i in L:
if i in tweet:
count = count + 1
return count
2。 def contains_hashtag(tweet,h): “”(str,str) - > bool
The first parameter represents a tweet, and second parameter represents a hashtag. Return True if and only if the tweet contains the hashtag.
>>> contains_hashtag('I like #csc120', '#csc120')
True
>>> contains_hashtag('I like #csc120', '#csc')
False
"""
if h in tweet:
return True
else:
return False