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

Nick Perkins nperkins7 at home.com
Mon Jul 9 00:28:55 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
> 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?
> 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.
>
> Thanks.
>
>


You should use the command prompt to play around
with things that you are not sure about.  ( if you don't, already )

The type(x) function is very useful,
as is the dir(x) fuction.
Also, a single underscore can be used to capture the result of the last
operation (see end of example).

Along with 'book learnin', interactive 'playing around' is the best way to
develop a clear picture of types and reference semantics, etc.  Plus, it's
fun!



eg.:

>>> s = "12.19.5.7.12"
>>> print s
12.19.5.7.12
>>> type(s)
<type 'string'>
>>> import string
>>> result = string.split(s,'.')
>>> print result
['12', '19', '5', '7', '12']
>>> type(result[0])
<type 'string'>
>>> result2 = map(string.atoi,result)
>>> print result2
[12, 19, 5, 7, 12]
>>> type(result2[0])
<type 'int'>
>>> string.atoi('12')
12
>>> type(_)
<type 'int'>

..etc..








More information about the Python-list mailing list