[Tutor] Replace sequence in list - looking for direction

Tiago Saboga tiagosaboga at gmail.com
Mon Jun 16 19:15:24 CEST 2008


On Mon, Jun 16, 2008 at 11:41:25AM -0400, GTXY20 wrote:
> Hello all,
> 
> Thanks in advance for any thoughts, direction, and guidance.
> 
> Let's say I have a list like the following:
> 
> a = ['a1','a2','a3','a4','a5','a6']
> 
> and then I have dictionary like the following:
> 
> b = {'a1,a2,a3':'super'}
> 
> I need some direction and thoughts on how I might seach the list for the
> string (the key in dict b) sequence in list a and replace the occurance with
> the value from dict b. At the end I would like to have list a represented
> as:
> 
> a = ['super', 'a4', 'a5', 'a6']

The following works for your example. I assume the values in the a
list are unique.

for key in b:
    keylist = key.split(',')
    if keylist < a:
        i = a.index(keylist[0])
        print a[:i] + [b[key]] + a[i+len(keylist):]

Tiago Saboga.


More information about the Tutor mailing list