gpt4 book ai didi

python - 列表Python中的重复元素

转载 作者:太空狗 更新时间:2023-10-29 21:16:50 26 4
gpt4 key购买 nike

假设我有一个字符串列表:

a = ['a', 'a', 'b', 'c', 'c', 'c', 'd']

我想列出至少连续出现两次的项目:

result = ['a', 'c']

我知道我必须使用 for 循环,但我不知道如何定位连续重复的项目。我该怎么做?

编辑:如果同一项目在 a 中重复两次怎么办?那么set函数就失效了

a = ['a', 'b', 'a', 'a', 'c', 'a', 'a', 'a', 'd', 'd']
result = ['a', 'a', 'd']

最佳答案

尝试 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's solution与其他替代方案相比,是最快的解决方案。

关于python - 列表Python中的重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13020679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com