python map string split list

后端 未结 5 699
盖世英雄少女心
盖世英雄少女心 2021-01-30 09:13

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

5条回答
  •  太阳男子
    2021-01-30 09:48

    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.

提交回复
热议问题