我尝试使用python的set()
方法来查找List中的唯一元素。它可以正常删除所有重复项。但这是我的要求,我想通过使用set()
方法获取被删除的元素。有人可以帮助我吗?
a=[1,2,3,1,4]
b=set(a)
Output:[1,2,3,4]
我的预期输出为[1]
。从set()
方法中删除的元素
答案 0 :(得分:2)
collections.Counter
在这里很有用。
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))
答案 1 :(得分: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}
答案 2 :(得分:2)
使用Counter
from collections import Counter
a = [1, 2, 3, 1, 4]
>>>[i for i in Counter(a) if Counter(a)[i] > 1]
[1]
答案 3 :(得分:1)
您可以扩展Set类(让您自己的Set类说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
答案 4 :(得分:1)
这将返回一个仅包含从原始集中删除的项目的集合:
>>> a = [1, 2, 3, 4, 1, 1, 5]
>>> set(i for i in a if a.count(i) > 1)
>>> {1}
答案 5 :(得分: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())
答案 6 :(得分:0)
简单的python示例,
for result in soup.select('.sold-results__normal-hit'):
nodes = result.select('.sold-property-listing__location h2 + div span')
if len(nodes)==2:
place = nodes[1].text.strip()
else:
place = 'not specified'
print(place)
产生,
def finder(s):
seen,yields=set(),set()
for i in s:
if i in seen and i not in yields:
yield i
yields.add(i)
else:
seen.add(i)
print(type(seen), seen )
a = [1,2,3,1,4]
print(list(finder(a)))