Re: find sublist inside list

2009-05-05 Thread Gerard Flanagan
Matthias Gallé wrote: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). For novelty value: from itertools import izip def

Re: find sublist inside list

2009-05-05 Thread mzdude
On May 4, 4:54 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Mon, 04 May 2009 15:12:41 -0300, mzdude jsa...@cox.net escribió: substring isn't limited to 0..255 substring = \0x%d\0x%d % (257,257) 'acaccgac'.replace(ac, substring)

Re: find sublist inside list

2009-05-05 Thread bearophileHUGS
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 --

find sublist inside list

2009-05-04 Thread Matthias Gallé
Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). If I do this with string ('acaccgac') I have the advantage of all the 'find'

Re: find sublist inside list

2009-05-04 Thread bearophileHUGS
Matthias Gallé: My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). There are several ways to solve this problem. Representing a string as a

Re: find sublist inside list

2009-05-04 Thread John O'Hagan
On Mon, 4 May 2009, Matthias Gallé wrote: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). li=['a', 'c', 'a', 'c', 'c', 'g', 'a',

Re: find sublist inside list

2009-05-04 Thread Francesco Guerrieri
On Mon, May 4, 2009 at 3:01 PM, John O'Hagan m...@johnohagan.com wrote: On Mon, 4 May 2009, Matthias Gallé wrote: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of

Re: find sublist inside list

2009-05-04 Thread bearophileHUGS
Matthias Gallé: the int that can replace a sublist can be 255, You didn't specify your integer ranges. Probably there are many other solutions for your problem, but you have to give more information. Like the typical array size, typical range of the numbers, how much important is total memory

Re: find sublist inside list

2009-05-04 Thread MRAB
Matthias Gallé wrote: bearophileh...@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,

Re: find sublist inside list

2009-05-04 Thread Matthias Gallé
bearophileh...@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'

Re: find sublist inside list

2009-05-04 Thread Aahz
In article gtmlsk$q5...@amma.irisa.fr, =?ISO-8859-1?Q?Matthias_Gall=E9?= mga...@irisa.fr wrote: My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result

Re: find sublist inside list

2009-05-04 Thread John O'Hagan
On Mon, 4 May 2009, Francesco Guerrieri wrote: On Mon, May 4, 2009 at 3:01 PM, John O'Hagan m...@johnohagan.com wrote: On Mon, 4 May 2009, Matthias Gallé wrote: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given

Re: find sublist inside list

2009-05-04 Thread mzdude
On May 4, 9:01 am, Matthias Gallé mga...@irisa.fr wrote: bearophileh...@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.

Re: find sublist inside list

2009-05-04 Thread Gabriel Genellina
En Mon, 04 May 2009 15:12:41 -0300, mzdude jsa...@cox.net escribió: substring isn't limited to 0..255 substring = \0x%d\0x%d % (257,257) 'acaccgac'.replace(ac, substring) '\x00x257\x00x257\x00x257\x00x257cg\x00x257\x00x257' This isn't what you think it is. Look carefully: py substring =

Re: find sublist inside list

2009-05-04 Thread Terry Reedy
Matthias Gallé wrote: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). If I do this with string ('acaccgac') I have the advantage of