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



完整代码:

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  

简单修复

为每个影片使用一个新的字典,并在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']}

相关内容

  • 没有找到相关文章

最新更新