#python
#python
Вопрос:
1|Toy Story (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Toy Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0
2|GoldenEye (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0
3|Four Rooms (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Four Rooms%20(1995)|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0
4|Get Shorty (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Get Shorty%20(1995)|0|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|0|0|0
5|Copycat (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Copycat%20(1995)|0|0|0|0|0|0|1|0|1|0|0|0|0|0|0|0|1|0|0
6|Shanghai Triad (Yao a yao yao dao waipo qiao) (1995)|01-Jan-1995||http://us.imdb.com/Title?Yao a yao yao dao waipo qiao (1995)|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0
7|Twelve Monkeys (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Twelve Monkeys%20(1995)|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|1|0|0|0
8|Babe (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Babe%20(1995)|0|0|0|0|1|1|0|0|1|0|0|0|0|0|0|0|0|0|0
9|Dead Man Walking (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Dead Man Walking%20(1995)|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0
10|Richard III (1995)|22-Jan-1996||://us.imdb.com/M/title-exact?Richard III%20(1995)|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|1|0
11|Seven (Se7en) (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Se7en%20(1995)|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|1|0|0
12|Usual Suspects, The (1995)|14-Aug-1995||http://us.imdb.com/M/title-exact?Usual Suspects, The%20(1995)|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|1|0|0
13|Mighty Aphrodite (1995)|30-Oct-1995||://us.imdb.com/M/title-exact?Mighty Aphrodite%20(1995)|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0
содержимое этого файла, местоположения соответственно;
movie id | movie title | release date | video release date | IMDb URL | unknown | Action | Adventure |
Animation | Children's | Comedy | Crime | Documentary | Drama | Fantasy | Film-Noir | Horror |
Musical | Mystery | Romance | Sci-Fi | Thriller | War | Western |
Числа определяют типы пленки
with open("u.item","r ") as f:
species= {}
list1=["a","b","c","d","","unknown","Action","Adventure","Animation","Children's","Comedy","Crime","Documentary","Drama","Fantasy","Film-Noir","Horror","Musical","Mystery","Romance","Sci-Fi","Thriller","War","Western"]
a = f.readlines()
for j in a:
h=j.split("|")
b=list(h)
d = 5
for g in range(19):
print(d)
for i in range(1):
if b[d]==1:
turler[list1[d]]={b[1]:b[d]}
d =1
чего я хочу :
species= {species1: {movie: 1, movie: 1, ...., movieN: 1}, species2: {movie2: 1, movie9: 1, ...., movieK: 1}, ... ...}
Комментарии:
1. это
csv
файл?2. расширение файла .item, а не CSV-файл
Ответ №1:
Вопрос: Фильтровать фильмы из
item: |0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0
Преобразуйте item: |0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0
в set()
.
Используйте set().methodes intersection|issuperset
для фильтрации фильмов по жанрам.
class Movie:
GENRE = ['unknown', 'Action', 'Adventure', 'Animation', "Children's", 'Comedy'] #...
def __init__(self, items):
"""
:param items: = movie id | movie title | ...
"""
self._items = items[:5]
self.id = items[0]
self.title = items[1]
self.genre = set()
for idx, g in enumerate(items[5:]):
if idx < len(Movie.GENRE) and g == '1':
self.genre.add(Movie.GENRE[idx])
class MovieDB:
def __init__(self):
self._movies = {}
def insert(self, movie):
self._movies[movie.id] = movie
def genre(self, _genre, op='issuperset'):
if not isinstance(_genre, (set)):
raise ValueError("Type 'set()' required, got '{}'.".format(type(_genre)))
print('genre{}'.format((_genre, op)))
result = []
for m in self._movies.values():
_op = getattr(m.genre, op)
if _op(_genre):
print('MATCH:{}'.format((m.title, m.genre)))
result.append(m)
return result
Использование:
movie_db = MovieDB()
with io.StringIO(ITEM) as fh_in:
for line in fh_in:
movie_db.insert(Movie(line.split('|')))
print('result:{}'.format(movie_db.genre({'Comedy','Animation'})))
print('result:{}'.format(movie_db.genre({'Comedy','Animation'}, op='intersection')))
Вывод:
genre({'Animation', 'Comedy'}, 'issuperset') MATCH:('Toy Story (1995)', {'Animation', 'Comedy', "Children's"}) result:[<__main__.Movie object at 0xf70b172c>] genre({'Animation', 'Comedy'}, 'intersection') MATCH:('Mighty Aphrodite (1995)', {'Comedy'}) MATCH:('Babe (1995)', {'Comedy', "Children's"}) MATCH:('Toy Story (1995)', {'Animation', 'Comedy', "Children's"}) MATCH:('Get Shorty (1995)', {'Action', 'Comedy'}) result:[<__main__.Movie object at 0xf70b19cc>, <__main__.Movie object at 0xf70b18ac>, <__main__.Movie object at 0xf70b172c>, <__main__.Movie object at 0xf70b17ec>]
Проверено с помощью Python: 3.4.2