用Python抓取一行的空格/缩进

Mik*_*den 13 python whitespace indentation

基本上,如果我有一行以缩进开头的文本,那么获取该缩进并将其放入Python中的变量的最佳方法是什么?例如,如果该行是:

\t\tthis line has two tabs of indention
Run Code Online (Sandbox Code Playgroud)

然后它将返回'\ t\t'.或者,如果该行是:

    this line has four spaces of indention
Run Code Online (Sandbox Code Playgroud)

然后它将返回四个空格.

所以我想你可以说我只需要从第一个非空白字符到结尾的字符串中删除所有内容.思考?

ken*_*ytm 25

import re
s = "\t\tthis line has two tabs of indention"
re.match(r"\s*", s).group()
// "\t\t"
s = "    this line has four spaces of indention"
re.match(r"\s*", s).group()
// "    "
Run Code Online (Sandbox Code Playgroud)

要剥离前导空格,请使用lstrip.


由于可能会质疑正则表达式的效率,我已经做了一些分析来检查每个案例的效率.

非常长的弦,非常短的前导空间

RegEx> Itertools >> lstrip

>>> timeit.timeit('r.match(s).group()', 'import re;r=re.compile(r"\s*")s="          hello world!"*10000', number=100000)
0.10037684440612793
>>> timeit.timeit('"".join(itertools.takewhile(lambda x:x.isspace(),s))', 'import itertools;s="          hello world!"*10000', number=100000)
0.7092740535736084
>>> timeit.timeit('"".join(itertools.takewhile(str.isspace,s))', 'import itertools;s="          hello world!"*10000', number=100000)
0.51730513572692871
>>> timeit.timeit('s[:-len(s.lstrip())]', 's="          hello world!"*10000', number=100000)
2.6478431224822998
Run Code Online (Sandbox Code Playgroud)

非常短的字符串,非常短的前导空间

lstrip> RegEx> Itertools

如果你可以将字符串的长度限制为chars或更少的字符,那么lstrip技巧可能更好.

>>> timeit.timeit('r.match(s).group()', 'import re;r=re.compile(r"\s*");s="          hello world!"*100', number=100000)
0.099548101425170898
>>> timeit.timeit('"".join(itertools.takewhile(str.isspace,s))', 'import itertools;s="          hello world!"*100', number=100000)
0.53602385520935059
>>> timeit.timeit('s[:-len(s.lstrip())]', 's="          hello world!"*100', number=100000)
0.064291000366210938
Run Code Online (Sandbox Code Playgroud)

这表明lstrip技巧大致为O(√n),如果前导空格的数量不多,则RegEx和itertool方法为O(1).

非常短的弦,非常长的领先空间

lstrip >> RegEx >>> Itertools

如果有很多前导空格,请不要使用RegEx.

>>> timeit.timeit('s[:-len(s.lstrip())]', 's=" "*2000', number=10000)
0.047424077987670898
>>> timeit.timeit('r.match(s).group()', 'import re;r=re.compile(r"\s*");s=" "*2000', number=10000)
0.2433168888092041
>>> timeit.timeit('"".join(itertools.takewhile(str.isspace,s))', 'import itertools;s=" "*2000', number=10000)
3.9949162006378174
Run Code Online (Sandbox Code Playgroud)

非常长的弦,非常长的领先空间

lstrip >>> RegEx >>>>>>>> Itertools

>>> timeit.timeit('s[:-len(s.lstrip())]', 's=" "*200000', number=10000)
4.2374031543731689
>>> timeit.timeit('r.match(s).group()', 'import re;r=re.compile(r"\s*");s=" "*200000', number=10000)
23.877214908599854
>>> timeit.timeit('"".join(itertools.takewhile(str.isspace,s))', 'import itertools;s=" "*200000', number=100)*100
415.72158336639404
Run Code Online (Sandbox Code Playgroud)

这表明如果非空间部分不是很多,所有方法大致缩放为O(m).

  • @Kenny:我想说的是*常见病例*比极端病例更重要.我认为所有这些测试都缺少真正的观点,因为这可能不是瓶颈而且可以在C中实现,如果是的话,为了比我们提出的任何纯Python更好的性能.(这不是我的downvote,顺便说一下,我只看到那个.) (2认同)

Phi*_*l H 12

偷偷摸摸的方式:虐待lstrip!

fullstr = "\t\tthis line has two tabs of indentation"
startwhites = fullstr[:len(fullstr)-len(fullstr.lstrip())]
Run Code Online (Sandbox Code Playgroud)

这样您就不必完成空白的所有细节了!

(感谢亚当的纠正)

  • 你可能意味着`[: - ...(..`. (2认同)