待處理數(shù)據(jù)如下:
0.2 0.3 0.5 1
0.3 0.1 0.2 3
0.4 0.2 0.3 1
0.2 0.2 0.2 2
0.3 0.3 0.3 2
0.3 0.2 0.6 3
0.1 0.1 0.1 4
如果最后一列相同(全是整數(shù)),則前面的列分別相加,然后按照最后一列排序(從小到大)
結(jié)果:
0.6 0.5 0.8 1
0.5 0.5 0.5 2
0.6 0.3 0.8 3
0.1 0.1 0.1 4
我的代碼(不含排序),這種解決方法不是很好,代碼思路是先合并行,在打?。ㄍㄟ^列舉的辦法)是否還有更好的辦法?
with open('1.txt', 'r') as f:
alist = []
d = {}
lines = f.readlines()
for line in lines:
line = line.strip().split()
alist.append(line)
for i in alist:
try:
d[i[3]] += i[:-1]
except KeyError:
d[i[3]] = i[:-1]
for course, score in d.items():
if len(score) > 3:
print course, float(score[0]) + float(score[3]), float(score[1]) + float(score[4]), \
float(score[2]) + float(score[5])
else:
print course, ' '.join(score)
歡迎選擇我的課程,讓我們一起見證您的進步~~
It is best to use libraries such as Pandas to implement this. I will reinvent the wheel according to your needs:
from collections import defaultdict
course_to_score = defaultdict(lambda: [0, 0, 0])
def add(line):
row = line.rstrip().split()
course_to_score[int(row[-1])] = map(sum, zip(course_to_score[int(row[-1])], map(float, row[: -1])))
with open('1.txt') as handle:
[add(line) for line in handle]
sorted(course_to_score.iteritems(), key=lambda item: item[0])
from collections import defaultdict
with open('1.txt', 'r') as f:
# 讀取文件的數(shù)據(jù)
data = [[float(item) for item in line.split()] for line in f]
# 合并最后一列相同的行
tmp_data = defaultdict(list)
[tmp_data[d[-1]].append(d) for d in data]
# 把最后一列相同的行的前面的列相加
new_data = [map(sum, zip(*lists)[0:-1]) + [lists[0][-1]] for lists in tmp_data.values()]
# 對新數(shù)據(jù)進行排序
new_data.sort(key=lambda x: x[-1])
print new_data