[英](Python) which is more efficient to split a string multiple separators? 1) Using multiple replace method then using split 2) using regular Expressions
例子:
(情况1)
#首先使用replace方法将不同类型的分隔符替换为单一类型,然后使用split方法
text = "python is, an easy;language; to, learn."
text_one_delimiter = text.replace("# ", ", ").replace("% ", ", ").replace("; ", ", ").replace("- ", ", ")
print(text_one_delimiter.split(", "))
(案例二)
#使用正则表达式进行拆分使用多个分隔符
import re
text = "python is# an% easy;language- to, learn."
print(re.split('; |, |# |% |- ', text))
timeit模块对于代码片段的速度比较很有用。 它可以通过以下方式使用:
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(取决于您的机器):
1.1250261999999793
2.2901268999999616
请注意,我从检查的代码中排除了print
并import re
,否则它会在不需要几次的情况下导入它。 结论是,在这种特殊情况下,具有多个.replace
的方法比re.split
更快。
(在 Python 3.7.3 中测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.