find sublist inside list

mzdude jsanga at cox.net
Mon May 4 20:12:41 CEST 2009


On May 4, 9:01 am, Matthias Gallé <mga... at irisa.fr> wrote:
> bearophileH... at lycos.com wrote:
> > John O'Hagan:
> >> li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c']
> >> for i  in range(len(li)):
> >>     if li[i:i + 2] == ['a', 'c']:
> >>         li[i:i + 2] = ['6']
>
> > Oh well, I have done a mistake, it seems.
> > Another solution then:
>
> >>>> 'acaccgac'.replace("ac", chr(6))
> > '\x06\x06cg\x06'
>
> > Bye,
> > bearophile
>
> Thanks bearophile and John for your quick answers.
> Unfortunately, the int that can replace a sublist can be > 255, but
> John's answer looks simple and good enough for me. I will use it as a
> starting point.
>

substring isn't limited to 0..255
>>> substring = "\0x%d\0x%d" % (257,257)
>>> 'acaccgac'.replace("ac", substring)
'\x00x257\x00x257\x00x257\x00x257cg\x00x257\x00x257'



More information about the Python-list mailing list