统计一行字母(以\n结尾),分别统计中英文字母、空格、数字和其它字符的个数

def CountWords(strLine):
  count = len(strLine)
  print 'The Length of strLine is ',count
  result = {'alpha':0,'space':0,'digit':0,'others':0}
  for i in xrange(count):
    if 'a'<=strLine[i]<='z' or 'A'<=strLine[i]<='Z':
      result['alpha'] += 1
    elif ' ' == strLine[i]:
      result['space'] += 1
    elif '\n' == strLine[i]:
      break
    elif '1'<=strLine[i]<='9':
      result['digit'] += 1
    else:
      result['others'] += 1

  return result

print CountWords('255.255.255.0\n is a ipv4 address')
print CountWords('255.255.255.a is not a ipv4 address.\n')
print CountWords('')
print CountWords("\0\n")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值