如何识别在 python 中从 set() 中删除的元素?
How to Identify the elements which are removed from set() in python?
我尝试使用 python 的 set()
方法来查找列表中的唯一元素。删除所有重复项效果很好。但这是我的要求,我想获取使用 set()
方法删除的元素。任何人都可以帮助我吗?
a=[1,2,3,1,4]
b=set(a)
Output:[1,2,3,4]
我的预期输出是 [1]
。从 set()
方法中删除的元素
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))
您甚至不需要设置。您想要对出现不止一次的每个元素进行计数。集合计数器和字典理解应该可以帮助您。
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}
您可以扩展 Set class(拥有自己的 Set class 说 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
这将 return 一个仅包含从您的原始集合中删除的项目的集合:
>>> a = [1, 2, 3, 4, 1, 1, 5]
>>> set(i for i in a if a.count(i) > 1)
>>> {1}
我认为您正在以一种稍微混淆的方式来解决问题。而不是试图让 set()
做一些它不打算做的事情(return 重复项列表)我会使用 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())
试试 Counter
from collections import Counter
a = [1, 2, 3, 1, 4]
>>>[i for i in Counter(a) if Counter(a)[i] > 1]
[1]
简单的python例子,
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)))
生产,
<class 'set'> {1, 2, 3, 4}
[1]
[Program finished]
我尝试使用 python 的 set()
方法来查找列表中的唯一元素。删除所有重复项效果很好。但这是我的要求,我想获取使用 set()
方法删除的元素。任何人都可以帮助我吗?
a=[1,2,3,1,4]
b=set(a)
Output:[1,2,3,4]
我的预期输出是 [1]
。从 set()
方法中删除的元素
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))
您甚至不需要设置。您想要对出现不止一次的每个元素进行计数。集合计数器和字典理解应该可以帮助您。
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}
您可以扩展 Set class(拥有自己的 Set class 说 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
这将 return 一个仅包含从您的原始集合中删除的项目的集合:
>>> a = [1, 2, 3, 4, 1, 1, 5]
>>> set(i for i in a if a.count(i) > 1)
>>> {1}
我认为您正在以一种稍微混淆的方式来解决问题。而不是试图让 set()
做一些它不打算做的事情(return 重复项列表)我会使用 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())
试试 Counter
from collections import Counter
a = [1, 2, 3, 1, 4]
>>>[i for i in Counter(a) if Counter(a)[i] > 1]
[1]
简单的python例子,
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)))
生产,
<class 'set'> {1, 2, 3, 4}
[1]
[Program finished]