python split() 与 rsplit() 性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21103433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:04:12  来源:igfitidea点击:

python split() vs rsplit() performance?

pythonstringsplit

提问by user1408351

I have a string in python. I want to split it with maxsplit = 1on separator which is pretty close to end of the string.

我在python中有一个字符串。我想用maxsplit = 1非常接近字符串末尾的分隔符来分割它。

For e.g.

例如

a = "abcdefghijklmnopqrstuvwxyz,1".

Will a.split(",", 1)be better in terms of performance than a.rsplit(",",1)?

a.split(",", 1)在性能方面比好a.rsplit(",",1)

采纳答案by user1408351

Below is a time test using timeit.timeitto compare the speeds of the two methods:

以下是timeit.timeit用于比较两种方法速度的时间测试:

>>> from timeit import timeit
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)')
1.6438178595324267
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)')
1.6466740884665505
>>>

As you can see, they are about equivalent. str.splitis a few fractions of a second faster, but that is really unimportant. So, you can pick whichever method you want.

如您所见,它们大致相同。 str.split快几分之一秒,但这真的不重要。所以,你可以选择你想要的任何方法。

P.S. Although, the str.splitmethod isone less character to type. :)

PS虽然,该str.split方法少一个字符键入。:)

回答by tchuncly

Just to complement @iCodez answer, you can run a timing test from the command-line:

为了补充@iCodez 的答案,您可以从命令行运行计时测试:

$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)'
1000000 loops, best of 3: 0.321 usec per loop
$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)'
1000000 loops, best of 3: 0.327 usec per loop

So, indeed, it's an irrelevant difference.

所以,实际上,这是一个无关紧要的区别。

回答by Kishore Avineni

Adding to the previous answers, using split vs rsplit should depend on where you want to search. Example:

添加到前面的答案中,使用 split 还是 rsplit 应该取决于您要搜索的位置。例子:

$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,sdfsgfkdjgherughieug,1".split(",")[2]'
1000000 loops, best of 3: 0.48 usec per loop
$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,sdfsgfkdjgherughieug,1".rsplit(",",1)[1]'
1000000 loops, best of 3: 0.453 usec per loop

Here you are searching for 1, in which case using rsplit is faster than split, whereas for the examples in the previous answers, split is faster.

在这里,您正在搜索 1,在这种情况下,使用 rsplit 比 split 快,而对于前面答案中的示例,split 更快。

回答by mVChr

I'm super late to this party, but for anyone else stumbling across this, partitionis faster than split(x, 1):

我参加这个聚会太晚了,但对于其他遇到这个问题的人来说,partition速度比split(x, 1)

>>> from timeit import timeit
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)')
0.23717808723449707
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)')
0.20203804969787598
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".partition(",")')
0.11137795448303223
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rpartition(",")')
0.10027790069580078

And you can ditch the ,easily if you want by h, _, t = s.rpartition(',')or such.

,如果您愿意,您可以轻松放弃h, _, t = s.rpartition(',')

回答by Dewy Duke

I think there is a slight difference between split()and rsplit(): for example:

我认为split()rsplit():之间存在细微差别,例如:

str1 = "w,e,l,c,o,m,e"
print(str1.split(',',2))

str1 = "w,e,l,c,o,m,e"
print(str1.rsplit(',',2))

You see, split()is used if you want to split strings on first occurrences and rsplit()is used if you want to split strings on last occurrences.

你看,split()如果你想在第一次出现时拆分字符串,rsplit()则使用它,如果你想在最后一次出现时拆分字符串,则使用它。