find sublist inside list

Matthias Gallé mgalle at irisa.fr
Mon May 4 10:01:02 EDT 2009


bearophileHUGS 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.

Thank's again.

-- 
Matthias Gallé
Project Symbiose
Centre de Recherche INRIA Rennes - Bretagne Atlantique,
Campus de Beaulieu, 35042 Rennes cedex, France
tel: (33|0) 2 9984 7523
http://www.irisa.fr/symbiose/matthias_galle



More information about the Python-list mailing list