我发现了一个有趣的事情,它比在分隔符之后获取整个子字符串partition更快。split我已经在 Python 3.5 和 3.6 (Cpython) 中进行了测试
In [1]: s = \'validate_field_name\'\n\nIn [2]: s.partition(\'_\')[-1]\nOut[2]: \'field_name\'\n\nIn [3]: s.split(\'_\', maxsplit=1)[-1]\nOut[3]: \'field_name\'\n\nIn [4]: %timeit s.partition(\'_\')[-1]\n220 ns \xc2\xb1 1.12 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\nIn [5]: %timeit s.split(\'_\', maxsplit=1)[-1]\n745 ns \xc2\xb1 48.8 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\nIn [6]: %timeit s[s.find(\'_\')+1:]\n340 ns \xc2\xb1 1.44 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 …Run Code Online (Sandbox Code Playgroud)