2 回答

TA贡献1808条经验 获得超4个赞
试试这个:
import re
c = ['.', '.', ';', ':', '!', '?']
top = ['asdd..,;;.:']
top_len = len(re.findall('['+''.join(c)+']',top[0]))

TA贡献1874条经验 获得超12个赞
您可以在元组中存储行和出现次数,并按字符串(行)中特定字符的数量增加出现次数值。
class Counter(object): # mutable object is needed to access it from tuple
def __init__(self, start_value=0):
self.value = start_value
def __str__(self):
return str(self.value)
c = ['.', '.', ';', ':', '!', '?']
top = []
with open(TEXT, "r") as fh:
for line in fh:
top.append((line.split("\n"), Counter(0)))
for line, occurrences in top:
for character in c:
occurrences.value += line.count(character)
添加回答
举报