[Tutor] newbie map help
karthik Guru
karthikg@aztec.soft.net
Mon, 19 Nov 2001 17:17:52 +0530
thanks!
i got this one to work.
map(lambda line: string.split(line,","),lines)
Obviously every line in lines is getting passed as a parameter to the
anonymous function.
karthik.
-----Original Message-----
From: Glen Wheeler [mailto:wheelege@tsn.cc]
Sent: Monday, November 19, 2001 3:33 PM
To: karthik Guru; tutor@python.org
Subject: Re: [Tutor] newbie map help
Howdy how,
> how do i use map here??
Well map takes a function and something to iterate over, then returns a
list. The problem here is that we need a function that will split a string
with the seperator ",". As you may have already realised, such a function
is string.split(lines[x], ","), where x is the index number of the list
lines.
Now you just need something to iterate over, and in this case you want a
bunch of index numbers - range(len(list)) should do fine.
So we have...
map(lambda x:string.split(lines[x], ","), range(len(lines)))
Which works fine. However it looks ugly, and I have somewhat unskillfully
dodged any explanation of lambda...which is deliberate - I will probably end
up confusing you even more :) Basically it creates a function which can be
passed as an argument to other things, very useful.
Anywho, there is probably a much cleaner and more elegant way of doing
this, but that is the one I came up with,
HTH,
Glen
>
> import string
> map(string.split, lines) ==> This s'd return me a list of lists.
>
> [["how","are","you","doing"],["ok fine"]..]
>
> How do i specify the separator character to the split function in map?
>
> thanks in advance,
> karthik
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>