find sublist inside list
tjreedy at udel.edu
Tue May 5 00:02:59 CEST 2009
Matthias Gallé wrote:
> My problem is to replace all occurrences of a sublist with a new element.
> 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' functions, but perfomance is bad and some extra care must be
> taken if one element consist of more then one character (case of 11 for
> So I really would like to work with lists straightforward, but I could
> not found anything to search a sublist inside a list.
> Any propositions for a simple solution?
For a mutable homogenous array, consider the array module.
Any algorithm that applies to a sequence of chars can be adjusted to
other sequences. For the above case, remember than you can easily
filter None out of a sequence. IE, replace 'a','c' with 6, None and
then filter when done.
More information about the Python-list