Python将Dict的值分配给列表

时间:2018-03-12 00:56:32

标签: python python-3.x list dictionary

问题

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

文本文件:

1.ratings

奔 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.books

道格拉斯亚当斯,搭便车的银河系指南 Richard Adams,Watership Down Mitch Albom,你在天堂遇见的五个人 Laurie Halse Anderson,说话 Maya Angelou,我知道为什么笼中的鸟唱歌

代码:

1.dict

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.ratings

{'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.books

["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.ratings

{'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'},...}

1 个答案:

答案 0 :(得分:1)

我的理解是你有一个评级文件(我们称之为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对的数组。