[Tutor] Transposing [n, [e],[s[te]],[d]] sequences
Mon, 18 Feb 2002 20:23:40 -0800 (PST)
On Mon, 18 Feb 2002, Danny Yoo wrote:
> On Sun, 17 Feb 2002, kevin parks wrote:
> > # Problem: how do i make something like this work for nested
> > # sequences? since sometimes we will have lists of lists or (god help
> > # us) list of *gasp* tuples...
> > By this i meant not first flattening the list or returning the list as
> > flat, but transposing each element leaving the structure in place.
> Well, one thing we can do is cheat.
> >>> import re
> >>> number_re = re.compile(r'[0-9]+')
> >>> l = [[0, 2], 4, [[, 7], 9], 11]
> >>> number_re.sub(str(l), '%s')
Yikes, I was supposed to edit that typo out from prying eyes. *grin*
Here's an explanation of the error: my plan was to replace all the numbers
with '%s' characters... but I had accidently reversed the order of the
arguments! Instead of replacing all instances of numbers in my list 'l',
instead I tried to replace all numbers in the string '%s' with 'l'.
That's why I get back '%s', and why I hastily reverse my arguments in the
next interpreter command.