在这里尝试itertools.groupby()
:
>>> from itertools import groupby,islice
>>> a = ['a', 'a', 'b', 'c', 'c', 'c', 'b']
>>> [list(g) for k,g in groupby(a)]
[['a', 'a'], ['b'], ['c', 'c', 'c'], ['b']]
>>> [k for k,g in groupby(a) if len(list(g))>=2]
['a', 'c']
使用islice()
:
>>> [k for k,g in groupby(a) if len(list(islice(g,0,2)))==2]
>>> ['a', 'c']
使用zip()
和izip()
:
In [198]: set(x[0] for x in izip(a,a[1:]) if x[0]==x[1])
Out[198]: set(['a', 'c'])
In [199]: set(x[0] for x in zip(a,a[1:]) if x[0]==x[1])
Out[199]: set(['a', 'c'])
timeit
结果:
from itertools import *
a='aaaabbbccccddddefgggghhhhhiiiiiijjjkkklllmnooooooppppppppqqqqqqsssstuuvv'
def grp_isl():
[k for k,g in groupby(a) if len(list(islice(g,0,2)))==2]
def grpby():
[k for k,g in groupby(a) if len(list(g))>=2]
def chn():
set(x[1] for x in chain(izip(*([iter(a)] * 2)), izip(*([iter(a[1:])] * 2))) if x[0] == x[1])
def dread():
set(a[i] for i in range(1, len(a)) if a[i] == a[i-1])
def xdread():
set(a[i] for i in xrange(1, len(a)) if a[i] == a[i-1])
def inrow():
inRow = []
last = None
for x in a:
if last == x and (len(inRow) == 0 or inRow[-1] != x):
inRow.append(last)
last = x
def zipp():
set(x[0] for x in zip(a,a[1:]) if x[0]==x[1])
def izipp():
set(x[0] for x in izip(a,a[1:]) if x[0]==x[1])
if __name__=="__main__":
import timeit
print "islice",timeit.timeit("grp_isl()", setup="from __main__ import grp_isl")
print "grpby",timeit.timeit("grpby()", setup="from __main__ import grpby")
print "dread",timeit.timeit("dread()", setup="from __main__ import dread")
print "xdread",timeit.timeit("xdread()", setup="from __main__ import xdread")
print "chain",timeit.timeit("chn()", setup="from __main__ import chn")
print "inrow",timeit.timeit("inrow()", setup="from __main__ import inrow")
print "zip",timeit.timeit("zipp()", setup="from __main__ import zipp")
print "izip",timeit.timeit("izipp()", setup="from __main__ import izipp")
输出:
islice 39.9123107277
grpby 30.1204478987
dread 17.8041124706
xdread 15.3691785568
chain 17.4777339702
inrow 11.8577565327
zip 16.6348844045
izip 15.1468557105
结论:
与其他替代方案相比,Poke 的解决方案是最快的解决方案。