[Tutor] Split string and convert to int

Lloyd Kvam pythontutor at venix.com
Wed Oct 29 22:11:44 EST 2003


I started a reply saying it didn't really matter, but changed my mind.  I should
have sent it.

My main code reviewer doesn't care for map/filter, but does like the
list comprehensions.  I find it easy to see the implied loops of list
comprehensions when scanning through programs.  map is easier to overlook.

Keep your code somewhat consistent and find what works for you.

David Mertz has a lot of useful recommendations in his book:
http://gnosis.cx/TPiP/
Text Processing in Python (a book)

Chapter one covers aspects of functional programming and uses map and filter
quite extensively.  Also read some of David Mertz's Charming Python columns.

mlong at datalong.com wrote:
> Thanks for the responses. Having the example using map has helped me to
> finally understand what it does. I had read about the map function in the
> docs but did not fully get it until now. I would like a little more
> clarification however. Lloyd prefers using list comprehension and Karl
> prefers the map function. Is this difference due to personal preference,
> or are there some other technical reasons for using one over the other.
> 
> Thanks,
> Mike
> 
> 
>>An unnamed person wrote:
>>
>>
>>>I have a string variable that represents a date. I want to pull out the
>>>individual parts as integers. Is there a more efficient way to do this?
>>
>>>fooDate='10/3/2003'
>>
>>>month, day, year = fooDate.split('/')
>>>month=int(month)
>>>day=int(day)
>>>year=int(year)
>>
>>In [9]: fooDate='10/3/2003'
>>
>>In [10]: [month, day, year] = map(int, fooDate.split('/'))
>>
>>In [11]: #or
>>
>>In [12]: [month, day, year] = [int(x) for x in fooDate.split('/')]
>>
>>
>>But in that case I would prefer map.
>>
>>   Karl
>>--
>>Please do *not* send copies of replies to me.
>>I read the list
>>
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list