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 -- http

Re: find sublist inside list

2009-05-05 Thread mzdude
On May 4, 4:54 pm, "Gabriel Genellina" wrote: > En Mon, 04 May 2009 15:12:41 -0300, mzdude 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

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 replace2(da

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 all

Re: find sublist inside list

2009-05-04 Thread Gabriel Genellina
En Mon, 04 May 2009 15:12:41 -0300, mzdude 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 = "\0x%d\0x%d"

Re: find sublist inside list

2009-05-04 Thread mzdude
On May 4, 9:01 am, 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. > > A

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 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'

Re: find sublist inside list

2009-05-04 Thread Aahz
In article , =?ISO-8859-1?Q?Matthias_Gall=E9?= 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 [6,6,'c','g',6]). What's your goal? After you do

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", c

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 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 Francesco Guerrieri
On Mon, May 4, 2009 at 3:01 PM, John O'Hagan 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 ['a','c']

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', '

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