【Python】统计字符串中英文、空格、数字、标点个数

题外话:今天打酱油的做了网易数据挖掘工程师的在线笔试题,被打击了。

本文代码可在 这里 下载。

问题

在网上无意间看到这么一个题目:统计一个字符串中的中英文、空格、数字、标点符号个数。
正好再熟悉一下 Python 中字符串相关方法,所以来做一下。

代码

# coding: utf-8

import string
from collections import namedtuple


def str_count(s):
    '''找出字符串中的中英文、空格、数字、标点符号个数'''
    
    count_en = count_dg = count_sp = count_zh = count_pu = 0
    s_len = len(s)
    for c in s:
        if c in string.ascii_letters:
            count_en += 1
        elif c.isdigit():
            count_dg += 1
        elif c.isspace():
            count_sp += 1
        elif c.isalpha():
            count_zh += 1
        else:
            count_pu += 1
    total_chars = count_zh + count_en + count_sp + count_dg + count_pu
    if total_chars == s_len:
        return namedtuple('Count', ['total', 'zh', 'en', 'space', 'digit', 'punc'])(s_len, count_zh, count_en, count_sp, count_dg, count_pu)
    else:
        print('Something is wrong!')
        return None
    return None


s = '上面是引用了官网的介绍,意思就是说 TensorBoard 就是一个方便你理解、调试、优化 TensorFlow 程序的可视化工具,你可以可视化你的 TensorFlow graph、学习参数以及其他数据比如图像。'
count = str_count(s)
print(s, end='\n\n')
print('该字符串共有 {} 个字符,其中有 {} 个汉字,{} 个英文,{} 个空格,{} 个数字,{} 个标点符号。'.format(count.total, count.zh, count.en, count.space, count.digit, count.punc))

将上面的程序保存到 str_count.py,然后执行测试下:

$ python str_count.py

上面是引用了官网的介绍,意思就是说 TensorBoard 就是一个方便你理解、调试、优化 TensorFlow 程序的可视化工具,你可以可视化你的 TensorFlow graph、学习参数以及其他数据比如图像。

该字符串共有 107 个字符,其中有 59 个汉字,36 个英文,6 个空格,0 个数字,6 个标点符号。

那个用于测试的字符串 s 源自 我的一篇关于 TensorBoard 的博文,首先输出原始字符串,然后输出中英文、空格、数字、标点符号各自的个数。

以后有好的想法再来优化这个程序,大家有什么好的想法也欢迎可以在评论区留言。

END

  • 15
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

secsilm

来一根火腿?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值