2015-03-25 37 views
6

我嘗試使用set() python方法來查找列表中的唯一元素。它可以很好地移除所有重複項。但這裏是我的要求,我想要通過使用set()方法來獲取要刪除的元素。任何人都可以幫助我嗎?如何識別在python中從set()中移除的元素?

a=[1,2,3,1,4] 
b=set(a) 
Output:[1,2,3,4] 

我的預期輸出是從set()方法

+2

如果什麼輸入'[1,2,3,1,4,1,1]'試試這個? – thefourtheye 2015-03-25 07:06:29

+3

得到他們的計數,並檢查計數> 1. – 2015-03-25 07:07:28

回答

1

你可以擴展集類(有自己的一套類說MYSET)和重寫此功能

def _update(self, iterable): 
    # The main loop for update() and the subclass __init__() methods. 
    data = self._data 

    # Use the fast update() method when a dictionary is available. 
    if isinstance(iterable, BaseSet): 
     data.update(iterable._data) 
     return 

    value = True 

    if type(iterable) in (list, tuple, xrange): 
     # Optimized: we know that __iter__() and next() can't 
     # raise TypeError, so we can move 'try:' out of the loop. 
     it = iter(iterable) 
     while True: 
      try: 
       for element in it: 
        data[element] = value 
       return 
      except TypeError: 
       transform = getattr(element, "__as_immutable__", None) 
       if transform is None: 
        raise # re-raise the TypeError exception we caught 
       data[transform()] = value 
    else: 
     # Safe: only catch TypeError where intended 
     for element in iterable: 
      try: 
       data[element] = value 
      except TypeError: 
       transform = getattr(element, "__as_immutable__", None) 
       if transform is None: 
        raise # re-raise the TypeError exception we caught 
       data[transform()] = value 
+0

的項目,所以你的答案基本上是「改變這些35行代碼中的東西」?... – shx2 2015-04-01 10:29:38

+0

是的,因爲這就是問題所在。對於那些已經使用Counter的人,我相信你已經給出了一個替代方案,而不是回答所問的問題(帶有額外的循環等)感謝投票順便說一句:) – 2015-04-01 10:37:48

2

collections.Counter除去[1] .The元件是有用這裏。

from collections import Counter 
counts = Counter(a) 
b = set(counts.keys()) 
for x, count in counts.items(): 
    if count > 1: 
     print('%d appearances of %s were removed in the set' % (count-1, x)) 
2

你甚至不需要設置。你需要對每個元素進行一次以上的計數。來自藏書的反駁與字典理解應該會讓你在那裏。

from collections import Counter 

a = [1, 1, 1, 2, 2, 3, 4]  
removed = {k: v-1 for k, v in Counter(a).iteritems() if v > 1} 

>>> removed 
Out[8]: {1: 2, 2: 1} 
1

這將返回一個僅包含項目的一組從原來的集合中刪除:

>>> a = [1, 2, 3, 4, 1, 1, 5] 

>>> set(i for i in a if a.count(i) > 1) 

>>> {1} 
1

我認爲你是以稍微混合的方式接近問題。而不是試圖讓set()做一些不打算做的事情(返回重複列表),我會用collections.Counter()來收集重複,然後從中獲取重複。

下面是一些代碼:

#!python 
from collections import Counter 
c = Counter([1,2,3,1,4]) 
dupes = [k for k,v in c.items() if v>1] 
b = set(c.keys()) 
2

Counter

from collections import Counter 
a = [1, 2, 3, 1, 4] 
>>>[i for i in Counter(a) if Counter(a)[i] > 1] 
[1]