Сравнение списка строк с другим списком строк - PullRequest
0 голосов
/ 12 января 2020
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 с помощью строкового объекта, но не смог l oop по всем строкам, содержащимся в списке positive_list.

Есть ли способ для l oop через target_list с positive_list?

1 Ответ

0 голосов
/ 12 января 2020

Вы можете использовать set для поиска совпадений:

target_set = set(target_list)
pos_set = set(positive_list)

matches = pos_set.intersection(target_set)
wordcount = len(matches)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...