简体   繁体   English

python map字符串拆分列表

[英]python map string split list

I am trying to map the str.split function to an array of string. 我试图将str.split函数映射到一个字符串数组。 namely, I would like to split all the strings in a string array that follow the same format. 也就是说,我想将字符串数组中的所有字符串拆分为相同的格式。 Any idea how to do that with map in python? 知道怎么用python中的map做到这一点? For example let's assume we have a list like this: 例如,假设我们有一个这样的列表:

>>> a = ['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']

want to split the strings by space ( split(" ")) using map to have a list as: 想要按空格分割字符串(split(“”))使用map将列表设置为:

>>> [['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']]

Though it isn't well known, there is a function designed just for this purpose, operator.methodcaller : 虽然它并不为人所知,但是有一个专门为此目的设计的功能, operator.methodcaller

>>> from operator import methodcaller
>>> a = ['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']
>>> map(methodcaller("split", " "), a)
[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']]

This technique is faster than equivalent approaches using lambda expressions. 这种技术比使用lambda表达式的等效方法更快。

map(lambda x: x.split(), a)但是,在这种情况下,使用列表[x.split() for x in a]更清晰。

Use map in conjunction with a function. map与函数结合使用。 A neat way is to use a lambda function: 一个简洁的方法是使用lambda函数:

>>> a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']
>>> map(lambda s: s.split(), a)
[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'],
 ['2011-12-20', '01:09:21']]

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 (ie not just a list of things that implement the split method in a way compatible with str ). 这仅在您知道有str列表时才有效(即不仅仅是以与str兼容的方式实现split方法的列表)。 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 (ie not tabs, newlines, or other whitespace), because you can't pass another argument using this method. 它还依赖于split()的默认行为,它在任何空格上分割,而不是使用x.split(' ') ,它只分割空格字符(即不是制表符,换行符或其他空格),因为你不能使用此方法传递另一个参数。 For calling behaviour more complex than this, I would use a list comprehension. 对于比这更复杂的调用行为,我会使用列表理解。

Community wiki answer to compare other answers given 社区维基回答比较给出的其他答案

>>> from timeit import Timer
>>> t = {}
>>> t['methodcaller'] = Timer("map(methodcaller('split', ' '), a)", "from operator import methodcaller; a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']")
>>> t['lambda'] = Timer("map(lambda s: s.split(), a)", "a = ['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']")
>>> t['listcomp'] = Timer("[s.split() for s in a]", "a = ['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']")
>>> for name, timer in t.items():
...     print '%s: %.2f usec/pass' % (name, 1000000 * timer.timeit(number=100000)/100000)
... 
listcomp: 2.08 usec/pass
methodcaller: 2.87 usec/pass
lambda: 3.10 usec/pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM