假设我有一些.csv
这样的数据:
query, score1, score2, score3
kobe bryant,0,3,1,
ccny,1,1,2,
lego,3,1,0,
disney,4,0,0,
power rangers,2,0,2,
britney spears,2,0,2,
backstreet boys,2,1,1,
soccer,3,0,1,
justin beaver,2,0,2,
new york knicks,2,1,1
在将分数加起来后,我希望得到类似的结果:
score1 = 10; score2 = 4; score3 18;
如何分拆并添加它们?
这是我到目前为止所拥有的:
import random
def getScores():
# open files to read
web = open("page.txt", "r");
img = open("image.txt", "r");
# scores for each search engine results
gScore = 0;
bScore = 0;
yScore = 0;
webDict = [];
imgDict = [];
# split by ','
tmp = img.read().split(",");
for i in range(0, len(tmp)-4, 4):
gScore = gScore + int(tmp[i+1]);
bScore = bScore + int(tmp[i+2]);
yScore = yScore + int(tmp[i+3]);
print "gScore is: ", gScore, "\n";
print "bScore is: ", bScore, "\n";
print "yScore is: ", yScore, "\n";
tmp = web.read().split(",");
for i in range(0, len(tmp)-4, 4):
gScore = gScore + int(tmp[i+1]);
bScore = bScore + int(tmp[i+2]);
yScore = yScore + int(tmp[i+3]);
print "gScore is: ", gScore, "\n";
print "bScore is: ", bScore, "\n";
print "yScore is: ", yScore, "\n";
if __name__ == "__main__":
getScores();
这会将所有分数加起来,但我在从数据创建字典时遇到问题。
我的意思是这样的:
bigList = [ 'query':{score1:int, score2:int, score3:int}, 'query2':{score1:int, score2:int, score3:int}... and so on];
答案 0 :(得分:3)
一旦将其拆分为逗号,就可以在一行中轻松处理:
gScore, bScore, yScore =
[sum(map(int, scores)) for scores in (data[n::4] for n in range(1, 4))]
data[::4]
部分从数据中获取每第4个项目,从每种类型的得分的适当偏移量开始。然后,将每种类型转换为整数并将它们相加。
答案 1 :(得分:1)
我先用逗号分割字符串:
stuff = 'kobe bryant,0,3,1,ccny,1,1,2,lego,3,1,0,disney,4,0,0,power rangers,2,0,2,britney spears,2,0,2,backstreet boys,2,1,1,soccer,3,0,1,justin beaver,2,0,2,new york knicks,2,1,1'
parts = stuff.split(',')
len(parts)
应该是4
的倍数,否则你可以抛出异常:
if len(parts)%4:
raise ValueError('bad csv')
然后执行以下操作:
d = {'score1': 0, 'score2': 0, 'score3': 0}
for i in range(len(parts)/4):
d['score1'] += int(parts[4*i+1])
d['score2'] += int(parts[4*i+2])
d['score3'] += int(parts[4*i+3])
print d
我得到了
{'score1': 21, 'score2': 7, 'score3': 12}