如何计算文本中的空格数或新行字符数,使连续空格仅计为一个? 例如,这非常接近我想要的:
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
。
答案 0 :(得分:4)
假设您被允许使用Python正则表达式;
import re
print len(re.findall(ur"[ \n]+", string))
快捷方便!
更新:此外,使用[\s]
代替[ \n]
来匹配任何空格字符。
答案 1 :(得分:3)
你可以这样做:
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)
答案 2 :(得分:2)
您可以遍历数字以将它们用作索引。
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)
注意第一个符号,因为此构造从第二个符号开始,以防止IndexError
。
答案 3 :(得分:2)
默认的str.split()函数会将连续的空格运行视为一个空格。因此,只需拆分字符串,获取结果列表的大小,然后减去一个。
len(string.split())-1
答案 4 :(得分:2)
你可以使用枚举,检查下一个char也不是空格,所以连续的空格只会算作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="") ))
答案 5 :(得分:1)
尝试:
def word_count(my_string):
word_count = 1
for i in range(1, len(my_string)):
if my_string[i] == " ":
if not my_string[i - 1] == " ":
word_count += 1
return word_count
答案 6 :(得分:1)
您可以使用函数 xmlns:app="http://schemas.android.com/apk/res-auto"
查找连续的空格组:
groupby()