J
J
Jungles2020-07-07 13:29:05
Python
Jungles, 2020-07-07 13:29:05

How to enter data into a table?

rank['contains_genre'] = [any( y in x for y in genres) for x in rank.genres]
genres-это входные данные

As a result, in the table we see either True or False, with the same data
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]

a c
c=[all( y in x for y in genres) for x in b]
вообще
[False, False, False, False]

It turns out already 4 values, but why then does one go to the table? It also depends on all and any, but they work incomprehensibly, because all returns True even if there is something in rank.genres that genre does not have.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kamenyuga, 2020-07-08
@kamenyuga

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)

We get - exactly what we need, according to my understanding of the task - in the first three there is the drama genre, the last one is in flight. And if you use it, it will work . {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 question

Ask a Question

731 491 924 answers to any question