- 论坛徽章:
- 2
|
统计字符串中各个字符的个数,代码如下
def countWords(strLine):
count={'alpha':0,'space':0,'digit':0,'others':0}
length=len(strLine)
for i in range(length):
if strLine[i] == '\n':
break;
if strLine[i].isdigit():
count['digit']+=1
elif strLine[i].isalpha():
count['alpha']+=1
elif strLine[i].isspace():
count['space']+=1
else:
count['others']=length-count['digit']-count['alpha']-count['space']
return {'alpha':count['alpha'],'space':count['space'],'digit':count['digit']
但是为什么执行后的字典顺序如下,和之前定位的时候的顺序不一样了呢?
{'alpha': 17, 'digit': 10, 'others': 5, 'space': 6} |
|