Python 计算文本中的空格数(将连续空格视为一个)
如何计算文本中空格或新行字符的数量,从而使连续空格仅计算为一个? 例如,这与我想要的非常接近:Python 计算文本中的空格数(将连续空格视为一个),python,python-3.x,spaces,Python,Python 3.x,Spaces,如何计算文本中空格或新行字符的数量,从而使连续空格仅计算为一个? 例如,这与我想要的非常接近: string = "This is an example text.\n But would be good if it worked." counter = 0 for i in string: if i == ' ' or i == '\n': counter += 1 print(counter) 但是,结果不应返回15,而应仅返回11只存储最后找到的字符。每次循
string = "This is an example text.\n But would be good if it worked."
counter = 0
for i in string:
if i == ' ' or i == '\n':
counter += 1
print(counter)
但是,结果不应返回
15
,而应仅返回11
只存储最后找到的字符。每次循环时将其设置为i。然后在内部if中,如果找到的最后一个字符也是空白字符,则不要增加计数器。您可以遍历数字以将其用作索引
for i in range(1, len(string)):
if string[i] in ' \n' and string[i-1] not in ' \n':
counter += 1
if string[0] in ' \n':
counter += 1
print(counter)
注意第一个符号,因为此构造从第二个符号开始,以防止索引器
您可以执行以下操作:
string = "This is an example text.\n But would be good if it worked."
counter = 0
# A boolean flag indicating whether the previous character was a space
previous = False
for i in string:
if i == ' ' or i == '\n':
# The current character is a space
previous = True # Setup for the next iteration
else:
# The current character is not a space, check if the previous one was
if previous:
counter += 1
previous = False
print(counter)
re
到re
scue
>>> import re
>>> string = "This is an example text.\n But would be good if it worked."
>>> spaces = sum(1 for match in re.finditer('\s+', string))
>>> spaces
11
这将消耗最少的内存,另一种构建临时列表的解决方案是
>>> len(re.findall('\s+', string))
11
如果您只想考虑空格字符和换行符(例如,与选项卡相反),请使用正则表达式<代码>(\n')+ '/c>而不是<代码> 's+'< /c> > < /p> 假设您允许使用Python正则表达式;
import re
print len(re.findall(ur"[ \n]+", string))
又快又简单
更新:此外,使用[\s]
而不是[\n]
来匹配任何空格字符。默认函数将连续运行的空格视为一个空格。因此,只需拆分字符串,得到结果列表的大小,然后减去一
len(string.split())-1
您可以使用enumerate,检查下一个字符也不是空白,因此连续的空白将仅计为1:
string = "This is an example text.\n But would be good if it worked."
print(sum(ch.isspace() and not string[i:i+1].isspace() for i, ch in enumerate(string, 1)))
您还可以使用带有生成器功能的iter
,跟踪最后一个字符并比较:
def con(s):
it = iter(s)
prev = next(it)
for ele in it:
yield prev.isspace() and not ele.isspace()
prev = ele
yield ele.isspace()
print(sum(con(string)))
itertools版本:
string = "This is an example text.\n But would be good if it worked. "
from itertools import tee, izip_longest
a, b = tee(string)
next(b)
print(sum(a.isspace() and not b.isspace() for a,b in izip_longest(a,b, fillvalue="") ))
尝试:
您可以使用函数
groupby()
查找连续空格组:
from collections import Counter
from itertools import groupby
s = 'This is an example text.\n But would be good if it worked.'
c = Counter(k for k, _ in groupby(s, key=lambda x: ' ' if x == '\n' else x))
print(c[' '])
# 11
索引到字符串[-1]可能不是最好的主意,结果也是15,而不是11。对不起,我把索引搞砸了。
和
之后的部分应该被索引为i-1
,而不是-1
,这是一个聪明的解决方案,但牺牲了代码的清晰性。这对于string=“这是一个示例文本。\n但如果它起作用就好了。”
或任何以空格结尾的行,正确答案是12,您需要len(string.split())-(不是字符串[-1].isspace())
这会处理换行符吗?@th3an0通常是这样。请格式化代码(使函数定义也格式化),并提供一些关于解决方案和结果的文字(关于一些示例数据)。
from collections import Counter
from itertools import groupby
s = 'This is an example text.\n But would be good if it worked.'
c = Counter(k for k, _ in groupby(s, key=lambda x: ' ' if x == '\n' else x))
print(c[' '])
# 11