2016-12-18 69 views
2

我有11個文件,其中每個包含學生和每個學生的分數。像這樣:結合元組列表

文件1: (A,3) (B,4) (C 1,1) (d,2) (E,6) (F,1)

文件二: (A,8) (B,3) (C,4) (d,2) 等,

我現在想使該遍歷所有11個文件,並增加了一個計數器並對所有鍵的值進行排序,以便我可以像這樣:

(A,11)(B,7)(E,6)(C,5)(d,4)(F,1)

我使用下面的代碼,但是這一次不組合從不同的文件中值:

import os                 
import glob                 
from collections import defaultdict 
from operator import itemgetter 

for file in list(glob.glob('*.txt')):          
    scores = [] 
with open(file) as f: 
    for line in f: 
     name, score = line.split(' ') 
     score = int(score) 
     scores.append((name, score)) 


##this is the part where it is all about: 
dict_1 = dict(scores) 
all_keys = [] 
all_keys += set(dict_1.keys()) 
sum_list = [(k, dict_1.get(k, 0)) for k in all_keys] 
print sorted(sum_list, key=itemgetter(1), reverse=True) 

有沒有一種方法,我可以從不同的文件在Python中添加鍵和值一起?

回答

1

您應該爲此使用collections.Counter。它會處理數據的組合,並允許您方便地從最高值到最低值對它們進行分類。另外,我認爲你的文件讀取塊有一個輕微的縮進問題。

import glob 
from collections import Counter 

counter = Counter() 

for filename in glob.glob('*.txt'): 
    with open(filename) as file: 
     for line in file: 
      name, score = line.split(' ') 
      counter[name] += int(score) 

print counter.most_common() 
+0

謝謝!這非常有幫助:) – cupcake93

0

也許不是最好的解決辦法,但試試這個(假設從兩個文件中的對在列表中scores):

from collections import defaultdict 

d = defaultdict(int) 
for s in scores: 
    d[s[0]] += s[1] 

print d 
0

這也許會做:

import os                 
import glob                 
from collections import defaultdict 

score_sums = defaultdict(int) 

for file_name in list(glob.glob('*.txt')):          
    with open(file_name, 'r') as f: 
     for line in f: 
      name, score = line.split(' ') 
      score = int(score) 
      score_sums[name] += score 

print sorted(score_sums, key=lambda k:score_sums[k], reverse=True) 

我固定縮進並通過使用您已經導入的defaultdict簡化了整個事情。我也用lambda替換了你的itemgetter,但這只是一個習慣問題。最後,我將file更改爲file_name,因爲file是內置類型。

+1

太棒了!這似乎工作:) – cupcake93

0

在解析文件時,使用字典並將每個鍵對應的分數相加可以起作用。元組的各個元素之間的逗號後沒有空格 - (A,3) (B,4) (C,1) (D,2) (E,6) (F,1):嘗試這樣的事情:

import os                 
import glob                 
from collections import defaultdict 
from operator import itemgetter 

scores = defaultdict(int) 
for file in list(glob.glob('*.txt')):          
    with open(file) as f: 
     for line in f: 
      tuples = line.split(' ') 
      for elem in tuples: 
       name, score = tuple(elem[1:-1].split(',')) 
       score = int(score) 
       scores[name] += score 

print scores 

另外,如果你想拆就喜歡split(' ')空格字符,你的文件應該包含這樣的數據。