如何将阅读信息添加到词典中?

2024-06-26 13:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

1|Toy Story (1995)|01-Jan-1995||://us.imdb.com/M/title-exact?Toy%20Story%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%20Rooms%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%20Shorty%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%20Monkeys%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%20Man%20Walking%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%20III%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%20Suspects,%20The%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%20Aphrodite%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}, ... ...}

Tags: incomhttpfortitlemoviejanexact
1条回答
网友
1楼 · 发布于 2024-06-26 13:28:46

Question: Filter movies from 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    

Usage:

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')))

Output:

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测试

相关问题 更多 >