Help With map() *He says while pulling his hair out*

Alex Martelli aleaxit at yahoo.com
Mon Jul 9 03:30:45 EDT 2001


"EricIDLE" <grayson-wilson at home.com> wrote in message
news:pM927.657463$166.13605776 at news1.rdc1.bc.home.com...
> Ok first off I have a wee problem with one of the examples in my book it
is
> as follows:
>
> s="12.19.6.7.12"
> ls = string.split(s, '.')
> md = map(string.atoi, ls)
>
> Ok. well the problem is basically the whole thing. I know s="12.19.6.7.12"
> is just defining the varible 's' with the numbers. But my first problem is

Specifically, variable s is being bound to a string made up of several
DIGITS and periods.  "Number" is an ambiguous terms, while "digit"
more precisely identifies one of the characters '0', '1', ... '9' (note
the quotes).

> after string.split in its argument I know what the S means but I dont know
> what the '.' means does that mean to remove all the periods?

No, it means to SPLIT where a period occurs.  The result of string.split
is then a list of strings, each a copy of the substring of s that occurs
between two periods.

> The second problem is that I dont quite grasp the map() function what
> exactly does it do, In its argument it says in lamens terms "Turn the
string
> ls into intergers" my problems is wasent it always intergers? I mean the

ls is a list of strings.  string.split always returns a list of strings --
specifically, copies of substrings of s.

> varible ls contains ['12', '19', '6', '7', '12'] arent those intergers
they

No, they are strings (which happen to be made up of digits, only, in
this case).  The quotes around each are a strong indication of their
string nature.

If you need to transform a string of digits into an integer number,
you can pass the string of digits to function int (or, as here used,
function string.atoi).  map() calls its first argument (normally a
function) over each item in its second argument (a sequence) and
returns the list of results  (map also has other uses, but this is
one fundamental use for it).


Alex






More information about the Python-list mailing list