[Tutor] Replace sequence in list - looking for direction

W W srilyk at gmail.com
Mon Jun 16 18:23:55 CEST 2008


On Mon, Jun 16, 2008 at 10:41 AM, GTXY20 <gtxy20 at gmail.com> wrote:
> 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']

That's a really odd problem...

If your dict key value will always be separated by a comma, the
easiest thing to do would use split on the key.

replacement_items = []
for x in b:
    replacement_items.append(x.split(','))

[['a1', ' a2', ' a3']] will be replacement_items.

You can simply iterate over those values and a.remove(each_value).

It took me 6 lines to write a function that will do what you're
requesting, though if you want 'super' to replace the consecutive(?)
string in place (i.e. a[0:2]) it would take a little more coding, but
not much.

HTH,
Wayne


More information about the Tutor mailing list