繁体   English   中英

如何在python中找到字符串中第一个非空白字符的索引?

[英]how to find the index of the first non-whitespace character in a string in python?

场景:

>>> a='   Hello world'
index = 3

在这种情况下,“H”指数为“3”。 但是我需要一个更通用的方法,这样对于任何字符串变量'a'需要我需要知道第一个字符的索引?

替代方案:

>>> a='\tHello world'
index = 1

如果你的意思是第一个非空白字符,我会用这样的东西......

>>> a='   Hello world'
>>> len(a) - len(a.lstrip())
3

另一个有点乐趣:

>>> sum(1 for _ in itertools.takewhile(str.isspace,a))
3

但是我愿意打赌第一个版本更快,因为它基本上是这个确切的循环,只在C中 - 当然,它需要在完成时构造一个新的字符串,但这基本上是免费的。


为了完整性,如果字符串为空或由完全空格组成,则这两个字符串都将返回len(a) (如果您尝试使用它进行索引,则无效)

>>> a = "foobar"
>>> a[len(a)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

使用regex

>>> import re
>>> a='   Hello world'
>>> re.search(r'\S',a).start()
3
>>> a='\tHello world'
>>> re.search(r'\S',a).start()
1
>>>

当字符串为空或仅包含空格时处理案例的函数:

>>> def func(strs):
...     match = re.search(r'\S',strs)
...     if match:
...         return match.start()
...     else:
...         return 'No character found!'
...     
>>> func('\t\tfoo')
2
>>> func('   foo')
3
>>> func('     ')
'No character found!'
>>> func('')
'No character found!'

你也可以尝试:

a = '   Hello world'
a.index(a.lstrip()[0])
=> 3

只要字符串包含至少一个非空格字符,它就会起作用。 我们可以更加小心,然后再检查一下:

a = '    '
-1 if not a or a.isspace() else a.index(a.lstrip()[0])
=> -1

另一种方法,只是为了好玩...使用特殊功能!

>>> def first_non_space_index(s):
    for idx, c in enumerate(s):
        if not c.isspace():
            return idx


>>> a = '   Hello world'        
>>> first_non_space_index(a)
3

根据mgilson的回答,您可以使用lstrip去除您想要的任何字符 -

unwanted = ':!@#$%^&*()_+ \t\n'
a= '  _Hello world'
res = len(a) - len(a.lstrip(unwanted)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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