Skip to main content
 首页 » 编程设计

Python 将字典的值分配给列表

2025年01月19日11telwanggs

问题

我有 2 个文本文件(ratings.txt 和 books.txt)。评分文件包含 Books 文件中书籍的所有评分(对于 books.txt 中的 55 本书,每个用户有 55 个评分)。我的代码创建了一个字典,用户作为 KEY,评级作为 VALUE,我还创建了一个书籍列表。但是我想创建单独的值,这样我就可以为每个用户分配给相应书籍的每个值创建一个字典。
之后我需要一个算法来推荐用户 A 和用户 B 都评论过的书籍(两个用户都不喜欢某本书或都喜欢某本书)。
我是否可以使用该方法输出个性化推荐或者我的方法效率不高。 这是否可能解决这个问题?如果是的话,我是不是把它弄得太复杂了?有什么简单的方法可以解决这个问题。

文本文件:

1.评分

本 5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 驼鹿 5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0

2.书籍

道格拉斯亚当斯,银河系漫游指南 理查德·亚当斯,Watership Down 米奇阿尔博姆,你在天堂遇见的五个人 劳里·哈尔斯·安德森,请说话 Maya Angelou,我知道笼中鸟为何歌唱

代码:

1.字典

filename = input("") 
ratings = [] 
with open(filename) as fp: 
    for line in fp: 
        ratings.extend(line.strip().split(',')) 
d = {ratings[i]:ratings[i+1] for i in range(0,len(ratings),2)} 
print(d) 

2. list

print ("\nReading the entire file into a list.") 
text_file = open("books.txt", "r") 
lines = text_file.readlines() 
lines[:] = [line.rstrip('\n') for line in lines] 
print (lines) 

输出:

1.评分

{'Ben': '5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0  
1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5', 'Moose': '5 5 0 0 0 0 3 0 0  
1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0  
0 0 5 5 0 3 0 0'...} 

2.书籍

["Douglas Adams,The Hitchhiker's Guide To The Galaxy", 'Richard  
Adams,Watership Down', 'Mitch Albom,The Five People You Meet in Heaven',  
'Laurie Halse Anderson,Speak', 'Maya Angelou,I Know Why the Caged Bird  
Sings', 'Jay Asher,Thirteen Reasons Why', 'Isaac Asimov,Foundation Series',  
'Ann Brashares,The Sisterhood of the Travelling Pants', 'Libba Bray,A Great  
and Terrible Beauty', 'Dan Brown,The Da Vinci Code'...] 

我想要的输出是:

1.评分

{'Ben': '5','0','0','0','0','0','0','1',...} 

1.并结合列表和字典

{'Ben':{'ratings':{"Douglas Adams,The Hitchhiker's Guide To The  
Galaxy":'5'},{"Richard Adams,Watership Down":'0'},{'Mitch Albom,The Five  
People You Meet in Heaven':'0'},{'Laurie Halse Anderson,Speak':'0'},...} 

请您参考如下方法:

我的理解是您有如下所示的评分文件(我们称它为 ratings.txt):

Ben 
5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 
Moose  
5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0 

并且您有如下所示的 books.txt 文件:

Douglas Adams,The Hitchhiker's Guide To The Galaxy  
Richard Adams,Watership Down 
Mitch Albom,The Five People You Meet in Heaven  
Laurie Halse Anderson,Speak  
Maya Angelou,I Know Why the Caged Bird Sings 

首先,您可以按照自己的方式阅读所有评分:

# Reading all the ratings from a file 
ratings = {} 
name = None 
with open("ratings.txt") as fp: 
    for line in fp: 
        line = line.strip() 
        if name is None: 
            name = line 
        else: 
            ratings[name] = map(int,line.split()) 
            name = None 
print (ratings) 

输出:

{'Moose': [5, 5, 0, 0, 0, 0, 3, 0, 0, 1, 0, 5, 3, 0, 5, 0, 3, 3, 5, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 3, 5, 0, 0, 0, 0, 0, 5, -3, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 5, 0, 3, 0, 0], 'Ben': [5, 0, 0, 0, 0, 0, 0, 1, 0, 1, -3, 5, 0, 0, 0, 5, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 1, 0, -5, 0, 0, 5, 5, 0, 5, 5, 5, 0, 5, 5, 0, 0, 0, 5, 5, 5, 5, -5]} 

然后你的阅读书籍列表的代码有点简化:

# Reading the entire file into a list 
with open("books.txt", "r") as books_file: 
    books = [line.rstrip('\n') for line in books_file] 
print (books) 

然后你可以用这种方式将它们合并在一起:

for name in ratings: 
    scores = ratings[name] 
    ratings[name] = {'ratings': dict(zip(books,scores))} 
print (ratings) 

输出:

{'Moose': {'ratings': {'Laurie Halse Anderson,Speak ': 0, "Douglas Adams,The Hitchhiker's Guide To The Galaxy ": 5, 'Mitch Albom,The Five People You Meet in Heaven ': 0, 'Richard Adams,Watership Down': 5, 'Maya Angelou,I Know Why the Caged Bird Sings': 0}}, 'Ben': {'ratings': {'Laurie Halse Anderson,Speak ': 0, "Douglas Adams,The Hitchhiker's Guide To The Galaxy ": 5, 'Mitch Albom,The Five People You Meet in Heaven ': 0, 'Richard Adams,Watership Down': 0, 'Maya Angelou,I Know Why the Caged Bird Sings': 0}}} 

请注意,输出中丢失了一些分数,因为我的文件 books.txt 只包含五本书,所以函数 zip 返回长度为 5 的对数组。