如何防止字典中的元素不断被更新?

How to prevent the elements in the dictionary constantly being updated?

完整代码如下:

import re

f = open('movies.item','r') 
# First three item of movies.item below:
#1|Toy Story (1995)|01-Jan-1995||http://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||http://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||http://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
empty_list = []
for items in f:
    new_item = re.sub(r'\n', '', items)
    empty_list.append(new_item)
movie_names = []
splitted_list = None
for i in range(len(empty_list)):

    splitted_list = empty_list[i].split("|")
    movie_names.append(splitted_list[1])

genres = ["Unknown", "Action", "Adventure", "Animation", "Children's","Comedy", "Crime", "Documentary", "Drama",
"Fantasy", "Film-Noir", "Horror", "Musical", "Mystery","Romance", "Sci-Fi", "Thriller", "War", "Western"]
genres.reverse()
genredict = {}
last_dict = {}
reversegenresum = []
for i in range(len(empty_list)):
    x = list(empty_list[i])
    claer_list = []
    for k in range(len(x)):
        if x[k] != "|":
            claer_list.append(x[k])

    claer_list.reverse()
    reverse_genre_data = claer_list[0:19]
    reversegenresum.append(reverse_genre_data)
    

for i in range(3): #trying for 3 movie
    for j in range(len(genres)): 
        if reversegenresum[i][j] == '1':      
            genredict[genres[j]] = '1'
        last_dict[movie_names[i]] = genredict    


print(last_dict) 

我想做什么? 我尝试匹配名为 'movies.item' 的文件中的数据。有电影及其数据信息,如“0|0|1|0”。如果数据的值等于 1 我需要将其与相应的类别匹配。但我只能为一部电影这样做。尽管我尝试以其他方式执行此操作时不会出现错误,但我的所有数据都是根据最后的数据进行调整的。如果你不明白我的意思,请复制代码自己试试

输入:

{'Toy Story (1995)': {'Comedy': '1', "Children's": '1', 'Animation': '1', 'Thriller': '1', 'Adventure': '1', 'Action': '1'}, 
 'GoldenEye (1995)': {'Comedy': '1', "Children's": '1', 'Animation': '1', 'Thriller': '1', 'Adventure': '1', 'Action': '1'}, 
 'Four Rooms (1995)': {'Comedy': '1', "Children's": '1', 'Animation': '1', 'Thriller': '1', 'Adventure': '1', 'Action': '1'}}  

我想要的:

{'Toy Story (1995)': {'Animation': 1, "Children's": 1, 'Comedy': 1}, 
 'GoldenEye (1995)': {'Action': 1, 'Adventure': 1, 'Thriller': 1}, 
 'Four Rooms (1995)': {'Thriller': 1},

问题

你一直在为每部电影保存完全相同的字典genredict

last_dict[movie_names[i]] = genredict  

简单修复

每部电影使用一个新的dict,在j循环之后分配它就足够了

for i in range(3):  
    genredict = {}
    for j in range(len(genres)):
        if reversegenresum[i][j] == '1':
            genredict[genres[j]] = '1'
    last_dict[movie_names[i]] = genredict

改善

你基本上有 4 个循环迭代同一件事:电影,而不是在每部电影上一个接一个地执行动作,而是在电影上一个接一个地执行它们

genres = ["Unknown", "Action", "Adventure", "Animation", "Children's", "Comedy", "Crime", "Documentary", "Drama",
          "Fantasy", "Film-Noir", "Horror", "Musical", "Mystery", "Romance", "Sci-Fi", "Thriller", "War", "Western"]
result = {}
with open('movies.item', 'r') as f:
    for items in f:
        index, name, date, url, _, *values = items.rstrip("\n").split("|")
        item_genre = dict(zip(genres, values))
        result[name] = {genre: value for genre, value in item_genre.items() if value == '1'}
  • 将行拆分一次并检索您需要的所有元素:名称和值
  • dict(zip( , )) 将流派与价值观配对
  • {genre: value for genre, value in item_genre.items() if value == '1'} 仅保留 1
  • 的流派

请注意,最后一行应该比以下内容更好,您不需要所有值都相同的字典 (1),只需保留一个列表

result[name] = [genre for genre, value in item_genre.items() if value == '1']

# {'Toy Story (1995)': ['Animation', "Children's", 'Comedy'], 'GoldenEye (1995)': ['Action', 'Adventure', 'Thriller'], 'Four Rooms (1995)': ['Thriller']}