[Tutor] newbie map help

Glen Wheeler wheelege@tsn.cc
Mon, 19 Nov 2001 21:02:31 +1100


  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
>