我正在尝试创建一种向用户提供个性化图书推荐的算法,该算法根据同龄人的分数预测用户可能喜欢哪本书。
用于解释的两个文本文件是"ratings.txt“和"books.txt",表示如下:
ratings.txt "Ben5 0 0 0 1 -3 5 0 0 0 5 5 0 0 0 1 0 -5 -5 0 0 5 5 5 0 0 0 5 5 5 Moose5 5 0 0 0 3 0 0 1 0 5 0 5 5-5 Moose5 5 0 0 0 3 0 0 0 5 -3 0 0 0 5-3 0 0 0(代表两个用户)
books.txt“道格拉斯·亚当斯,”银河漫游者指南“理查德·亚当斯,”水船下米奇·阿尔博姆“,”你在天堂遇到的五个人“,讲玛雅·安杰鲁,”我知道为什么笼中的鸟在歌唱“”(四本书和作者代表)
评级系统从-5(真的不喜欢)到0(没有看)到5(真的很喜欢)。
我将如何将这两个文件上传到Python中,作为用于此相似算法的字典?
提前谢谢你。
发布于 2014-03-22 16:39:12
读取文件,遍历行,逐行拆分。键是带有0索引的项,值-索引1及更多项的列表:
data = {}
with open('test.txt', 'r') as f:
for line in f:
line_data = line.split()
data[line_data[0]] = line_data[1:]
print data指纹:
{'Ben5': ['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'],
'Moose5': ['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']}此外,如果您希望数字为整数:
data[line_data[0]] = map(int, line_data[1:])希望这能有所帮助。
发布于 2014-03-22 16:43:04
你提供的这么少,但需要从某个地方开始。我假设您的文件--单词是键,其余的是要存储在列表中的值--如果每个观察都是由换行符定义的,那么alecxe的解决方案就能工作了
我正在修改它,以便以字符串的形式读取输入文件,并在每一步中添加了一个条带()方法,以确保记录了行中断。
from collections import defaultdict
test = open(someFile).read() # this
mydict = defaultdict(list)
from collections import defaultdict
for item in test.split():
try:
x = int(item.strip())
mydict[currentKey].append(x)
except ValueError:
currentKey = item.strip()
mydict[currentKey] = []
defaultdict(<type 'list'>, {'Ben5': [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], 'Moose5': [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]})发布于 2014-03-22 17:04:12
用正则表达式和回复试一试
import re
### assume read all lines from a file
input = '''Ben5 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 Moose5 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 9
Moos7 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'''
output = {}
for (key, val) in re.findall("([a-zA-Z]\w+)\s+(.*?)(?=[a-zA-Z]|$)", input, re.M):
output[key] = val
print outputhttps://stackoverflow.com/questions/22580381
复制相似问题