I am trying to map the str.split
function to an array of string. namely, I would like to split all the strings in a string array that follow the same format. Any id
This is how I do it:
>>> a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']
>>> map(str.split, a)
[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']]
This only works when you know you have a list of str
(i.e. not just a list of things that implement the split
method in a way compatible with str
). It also relies on using the default behaviour of split()
, which splits on any whitespace, rather than using x.split(' ')
, which splits on space characters only (i.e. not tabs, newlines, or other whitespace), because you can't pass another argument using this method. For calling behaviour more complex than this, I would use a list comprehension.