简体   繁体   中英

(Python) which is more efficient to split a string multiple separators? 1) Using multiple replace method then using split 2) using regular Expressions

Example:

(Case 1)

#first using replace method to replace different type of separator to single type then using split method

text = "python is, an easy;language; to, learn."
text_one_delimiter = text.replace("# ", ", ").replace("% ", ", ").replace("; ", ", ").replace("- ", ", ")

print(text_one_delimiter.split(", "))

(case 2)

#Using regular expression for splitting using multiple separators

import re

text = "python is# an% easy;language- to, learn."
print(re.split('; |, |# |% |- ', text))

timeit module is useful for speed comparison of code snippet. It might be used following way:

import timeit
case1 = '''text = "python is, an easy;language; to, learn."
text_one_delimiter = text.replace("# ", ", ").replace("% ", ", ").replace("; ", ", ").replace("- ", ", ")
text_one_delimiter.split(", ")'''
case2_setup = "import re"
case2 = '''text = "python is# an% easy;language- to, learn."
re.split('; |, |# |% |- ', text)'''
print(timeit.timeit(case1))
print(timeit.timeit(case2,case2_setup))

Output (will depend on your machine):

1.1250261999999793
2.2901268999999616

Note that I excluded print s from examined code and make import re setup, as otherwise it would import it without need several time. Conclusion is that in this particular case method with multiple .replace s is faster than re.split .

(tested in Python 3.7.3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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