Answer the question
In order to leave comments, you need to log in
How to enter data into a table?
rank['contains_genre'] = [any( y in x for y in genres) for x in rank.genres]
genres-это входные данные
genres=['Drama', 'Crime']
b=['Drama', 'Action', 'Crime', 'Thriller']
c=[any( y in x for y in genres) for x in b]
с
[True, False, True, False]
c=[all( y in x for y in genres) for x in b]
вообще
[False, False, False, False]
Answer the question
In order to leave comments, you need to log in
There are clearly problems in the logic of comparing sets of values. Why 2 loops together with the operator in
? Something obviously superfluous - either the operator, or one of the cycles. And then it checks for the presence of a string in a string, and not in a set of strings.
Here is an example fix:
# хотим отобрать фильмы с такими жанрами - хотя бы одним, но не обязательно всеми сразу
genres_to_look_for = ['Drama', 'Crime']
# вот набор фильмов и их жанров
movies_and_genres = {
1: ['Drama', 'Action', 'Crime', 'Thriller'],
2: ['Drama'],
3: ['Drama'],
4: ['Comedy']}
# для каждого фильма проверяем наличие у него искомых жанров (хотя бы одного)
# если any заменить на all, то будет проверяться одновременное наличие всех искомых жанров
movies_filtered = {
movie: any((genre in genres) for genre in genres_to_look_for)
for movie, genres in movies_and_genres.items()
}
print(movies_filtered)
{1: True, 2: True, 3: True, 4: False}
all
{1: True, 2: False, 3: False, 4: False}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question