map - lambda - problem

Gregor Lingl aon.912502367 at aon.at
Sun Mar 11 12:24:40 CET 2001


I tried to program a function, that rotates a piece of a puzzle in the
following way:

I represented a (rectangular) piece to rotate

abc              cfil
def      ===>    behk
ghi              adgj
jkl

as a list:

>>> n = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k',
'l']]

In direct mode I got:

>>> r = []
>>> for i in range(len(n[0])-1,-1,-1):
        r.append(map(lambda x: x[i],n))

>>> r
[['c', 'f', 'i', 'l'], ['b', 'e', 'h', 'k'], ['a', 'd', 'g', 'j']]

as intended.

If i put this into a function:

def rotate(piece):
    r = []
    for i in range(len(piece[0])-1,-1,-1):
        r.append(map(lambda x: x[i], piece))
    return r

I get

>>> rotate(n)
[['a', 'd', 'g', 'j'], ['a', 'd', 'g', 'j'], ['a', 'd', 'g', 'j']]
>>>

... as if i always had the value 0.

Why, exactly, this behaviour. Is i now taken from some other namespace?
If so, which one and why is it initialized with 0?
How can I work around this?

Thanks, Gregor Lingl

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20010311/8f6c7e1d/attachment.html>


More information about the Python-list mailing list