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

Remco Gerlich scarblac at pino.selwerd.nl
Mon Jul 9 02:17:34 EDT 2001


EricIDLE <grayson-wilson at home.com> wrote in comp.lang.python:
> 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
> 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?

It splits the string at the periods, turning "12.19.6.7.12" into
["12","19","6","7","12"]. If you give it the argument "1", it will split at
whatever places the 1s are, yielding ["","2.","9.6.7.","2"].

> 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
> varible ls contains ['12', '19', '6', '7', '12'] arent those intergers they
> are numbers. Any ways if you could reply answering all the questions in
> whole or maybe just a few of em' it would be great.

'12' is not a number, it's a string. It consists of two characters, '1' and
'2'. If you add it to another string, it'll just concatenate them:

>>> '12' + '19'
'1219'

12 is a number (no quotes!). string.atoi() turns '12' into 12.

The map() turns every string into a number and returns the list of results.

-- 
Remco Gerlich



More information about the Python-list mailing list