2016-01-25 35 views
0

hey iam使用python版本2.5.1。在csv文件中的多個鍵上找到重複計數和記錄python

您好想要統計整個文件中的重複記錄和記錄。誰能幫我 。計數不應包含任何Counter或OrderedDict函數。以上兩個功能是不存在在Python 2.5.1版本

dup_s_output = [] 
seen=set() 

for row1 in sort_src: 
#print (row1) 
    if row1 in seen : 
     dup_s_output.append(row1) 
    seen.add(row1) 
+2

您應該使用字典而不是兩個列表。 – SirParselot

+1

如果您不介意使用其他庫,'pandas'可以輕鬆讀取csv文件並具有'DataFrame.duplicated'功能,可以非常輕鬆地識別重複項。 – Tgsmith61591

+1

什麼是關鍵?整行或每行內的一組列?你之後有什麼 - 計算每個重複的行或所有的dups的數量? – mhawke

回答

0

如果條目的順序都沒有,我建議使用字典來跟蹤復發的重要:

sort_src = list("hello world") #for testing 
seen = {} 
for row1 in sort_src: 
    seen[row1] = seen.get(row1,0) + 1 
    #if the row is already in the dict then it's value is increased by 1 
    #if the row is not in the dict then .get() returns 0 (then add 1 

for r in seen: 
    print(r,"occured",seen[r],"times") 

如果行的順序也沒關係一樣可以用collections.OrderedDict()

import collections 
seen = collections.OrderedDict() 

應用兩種方式seen.keys()會給你唯一條目和的列表將是一個(entry,count)元組列表。

編輯 - 僅計算你需要總結的總條目副本的數目(dict.values()),然後減去獨特的條目數(字典的LEN)

num_of_dup_entries = sum(seen.values()) - len(seen) 
+0

請注意,這隻會在row1是可哈希對象時起作用,但同樣的限制適用於您正在使用的集合的使用,所以我不認爲這會妨礙您。 –

+0

從上面的代碼m獲得計數,但我重複的記錄不會來任何人可以幫助我下面這是我的代碼f = open('bravo_temp_src24.csv','rb') c = Counter(key(row)for如果t [1]> 1] #或者,如果您更喜歡字典 dups_dict = [c.most_common()if t [1]> 1] ptr1 = c.most_common() dups_dict = {row:count for row,count in c.most_common()if count> 1} –

+0

也許這個編輯就是你要找的東西? –

0

的標題該問題提到了「多重密鑰」,我假定該密鑰由CSV字段的子集組成。由於您在計數後可以使用collections.Counter

import csv 
from operator import itemgetter 
from collections import Counter 

key = itemgetter(0,2,4) # for example: columns 0, 2 and 4 comprise the key 

with open('data.csv') as f: 
    c = Counter(key(row) for row in csv.reader(f)) 
    dups = [t for t in c.most_common() if t[1] > 1] 
    # or, if you prefer a dict 
    dups_dict = {row: count for row, count in c.most_common() if count > 1} 

或者,如果你是比較整行的關鍵可能是每行:

from collections import Counter 

with open('data.csv') as f: 
    c = Counter(f) 
    dups = [t for t in c.most_common() if t[1] > 1] 
    dups_dict = {row: count for row, count in c.most_common() if count > 1} 

在這兩種情況下,上面我用Counter.most_common()作爲一種方便的方式來訂購dups名單由降計數頻率。如果這不重要,或者您正在生成dups_dict,那麼您可以僅使用Counter.items(),因爲沒有固有順序。

+0

上面的代碼整行不工作的關鍵是工作,我一個doudt如何提取只有計數值的整個文件幫助我在這 –

+0

以什麼方式「整個行「代碼不起作用?至於計數值,你想要每個重複計數還是重複的總數?你甚至需要保持線路,還是隻需要點數? – mhawke

+0

我想寫入重複到另一個文件和重複計數顯示 –

0
len(dup_s_output) 

以上將返回列表中的項目數。

0

它可以幫助有一個基本的反類在Python 2.5的使用方法:

class BasicCounter(dict): 
    def update(self,iterable): 
     for thing in iterable: 
      self[thing] = self.get(thing,0) + 1 

    def __init__(self,iterable=None): 
     dict.__init__(self) 
     if iterable: 
      self.update(iterable) 

所有其他張貼的答案都指望一個行內複製rows,而不是條目,以獲取各行的計數分別您可以使用此:

row_counts = [] 
for row in sort_src: 
    row_count.append(BasicCounter(row)) 

或者完全忽略行的分離和計數在整個文件中的重複的條目您可以使用此:

entry_count = BasicCounter() 
for row in sort_src: 
    entry_count.update(row) 

希望這些之一是你在找什麼!

相關問題