df <- structure(list(Region = structure(1:2, .Label = c("Australia",
"Norway"), class = "factor"), `2000` = c(15.6, 19.05), `2001` = c(18.4,
20.2), `2002` = c(19.2, 15.3), `2003` = c(20.2, 10), `2004` = c(39.1,
10.1), `2005` = c(50.2, 5.6)), class = "data.frame", row.names = c(NA, -2L))
positive_list = [line.strip() for line in open('positive-words.txt')] #converts text file to list
target_list = ['title', 'trump', 'impeached', 'for', 'abuse', 'of', 'power', 'two', 'weeks', 'before']
def wordcount(target_list, target_string):
counter = 0
for string in target_list:
if string == target_string:
counter += 1
else:
print('No matches found')
break
print('Number of matches: ' + counter)
wordcount(target_list, positive_list[x])
我能够使用字符串对象搜索target_list,但无法遍历列表positive_list中包含的整个字符串。
有没有一种方法可以通过positive_list遍历target_list?
答案 0 :(得分:1)
您可以使用set
查找匹配项:
target_set = set(target_list)
pos_set = set(positive_list)
matches = pos_set.intersection(target_set)
wordcount = len(matches)