def list_duplicates(seq):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
seen_twice = set( x for x in seq if x in seen or seen_add(x) )
# turn the set into a list (as requested)
return list( seen_twice )
a = [1,2,3,2,1,5,6,5,5,5]
list_duplicates(a) # yields [1, 2, 5]
为了以防万一,这里有一些时间安排:
# file: test.py
import collections
def thg435(l):
return [x for x, y in collections.Counter(l).items() if y > 1]
def moooeeeep(l):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
seen_twice = set( x for x in l if x in seen or seen_add(x) )
# turn the set into a list (as requested)
return list( seen_twice )
def RiteshKumar(l):
return list(set([x for x in l if l.count(x) > 1]))
def JohnLaRooy(L):
seen = set()
seen2 = set()
seen_add = seen.add
seen2_add = seen2.add
for item in L:
if item in seen:
seen2_add(item)
else:
seen_add(item)
return list(seen2)
l = [1,2,3,2,1,5,6,5,5,5]*100
结果如下:(干得好@johnlaroy!)
$ python -mtimeit -s 'import test' 'test.JohnLaRooy(test.l)'
10000 loops, best of 3: 74.6 usec per loop
$ python -mtimeit -s 'import test' 'test.moooeeeep(test.l)'
10000 loops, best of 3: 91.3 usec per loop
$ python -mtimeit -s 'import test' 'test.thg435(test.l)'
1000 loops, best of 3: 266 usec per loop
$ python -mtimeit -s 'import test' 'test.RiteshKumar(test.l)'
100 loops, best of 3: 8.35 msec per loop
a = [[1], [2], [3], [1], [5], [3]]
no_dupes = [x for n, x in enumerate(a) if x not in a[:n]]
print no_dupes # [[1], [2], [3], [5]]
dupes = [x for n, x in enumerate(a) if x in a[:n]]
print dupes # [[1], [3]]
你不需要数数,只要看看以前有没有看到过。使that answer适应于此问题:
为了以防万一,这里有一些时间安排:
结果如下:(干得好@johnlaroy!)
有趣的是,除了计时本身,使用pypy时排名也会略有变化。最有趣的是,基于计数器的方法从pypy的优化中受益匪浅,而我建议的方法缓存方法似乎几乎没有效果。
显然,这种效果与输入数据的“重复性”有关。我已经设置了
l = [random.randrange(1000000) for i in xrange(10000)]
,得到了以下结果:要删除重复项,请使用
set(a)
。要打印副本,请执行以下操作:请注意,
Counter
不是特别有效(timings),这里可能有过度杀伤力。set
会表现得更好。此代码按源顺序计算唯一元素的列表:或者,更简洁地说:
我不推荐后一种样式,因为
not seen.add(x)
做什么并不明显(setadd()
方法总是返回None
,因此需要not
)。要计算不带库的重复元素列表,请执行以下操作:
如果列表元素不可哈希,则不能使用sets/dict,必须使用二次时间解(将每个时间解进行比较)。例如:
相关问题 更多 >
编程相关推荐