1. 程式人生 > >[python] 查詢列表中重複的元素

[python] 查詢列表中重複的元素

已有列表:

a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5]

寫法1:

import collections
print([item for item, count in collections.Counter(a).items() if count > 1])
## [1, 2, 5]

但是 Counter 效率不高,寫法2:

seen = set()
uniq = []
for x in a:
    if x not in seen:
        uniq.append(x)
        seen.add(x)

但是上面的code,結果不對,我改成了下面這樣:

seen = set()
duplicated = set()
for x in a:  
    if x not in seen:  
        seen.add(x)
    else:
        duplicated.add(x)
print(duplicated)  # {1, 2, 5}

第3種寫法比第2種寫法更為緊湊,但這種寫法不那麼直觀:

seen = set()
uniq = [x for x in a if x not in seen and not seen.add(x)]  # 這種寫法,結果也不對

我自己的寫法:

a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5]
b = set(a)
for each_b in b:
    count = 0
    for each_a in a:
        if each_b == each_a:
            count += 1
    print(each_b, ": ", count)

執行如下:
1 :  2
2 :  2
3 :  1
5 :  4
6 :  1


或者這樣寫:

a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5]
duplicated = set()  
for i in range(0, len(a)):
    if a[i] in a[i+1:]:
        duplicated.add(a[i])
print(duplicated)
# {1, 2, 5}

用下面的函式還可以列出重複元素的索引:

a = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5]
source = a
from collections import defaultdict
def list_duplicates(seq):
    tally = defaultdict(list)
    for i,item in enumerate(seq):
        tally[item].append(i)
    return ((key,locs) for key,locs in tally.items() 
                            if len(locs)>1)

for dup in sorted(list_duplicates(source)):
    print(dup)

輸出如下:
(1, [0, 4])
(2, [1, 3])
(5, [5, 7, 8, 9])