rprt.net
当前位置:首页 >> python统计字母个数 >>

python统计字母个数

都是字母的话可以简单这样统计个数 a=["abc","def","ghi"] count=0 for i in a: count+=len(i) print(count)---9 要拆开,用list()函数就可以了.a=["abc","def","ghi"] b=[] for i in a: b.append(list(i)) print(b)---[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

1234567 l =0 with open('file.txt', 'r') as fr: txt =fr.read() forc intxt: ifc in'abcdefghijklmnopqrstuvwxyzabcdefghjijklmnopqrstuvwxyz': l +=1 print(l)

# python中输入字符串,统计字符串中大小写英文字母各有多少个?class countNum(object):def __init__(self):s = input("input a string") self.s = s def judge(self):bigger = 0 smaller = 0 for i in self.s:if i < 'A' or i > 'z':continue elif 'A' < i < 'Z':bigger +=

import string def letter_frequency(s): s = s.lower() #全部2113转小写5261 l = [] for i in string.lowercase: l.append(s.count(i)) #统计4102个数 return l #返回1653 运行专 >>> print(letter_frequency('Asdasdad')) 结果属 [3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]

要用len

使用sum配合列表表达式:s = '14.52 67 48.9' print(sum(1 if _ in '0123456789'else 0 for _ in s'))使用正则匹配:import re s = '14.52 67 48.9' print(len(re.findall('\\d', s)))

s='sfawaef#$#%fdf1232' d = dict((i, s.count(i)) for i in set(s) if i.isalpha())或 d = {i: s.count(i) for i in set(s) if i.isalpha()}或 from string import ascii_letters d = {i: s.count(i) for i in set(s) if i in ascii_letters}或 from collections import Counter from string

下面来是对源应百的度code inputStr ="XXXdsfdssd" aa = inputStr.upper() dict ={} for each in range(len(aa)):if aa[each] in dict:dict[aa[each]] +=1 else:dict[aa[each]] =1 aar = sorted(dict.items(), key=lambda item:item[1]) print (aar[-1])

定义一个函数,给定一个字符串参数,计算每个单词出现在字符串总次数 不区分大小写 .还有其他什么要求没看懂 ##python 2.7 windowswords =raw_input (' please enter words:')countdict={}for word in words.strip(): word=word.lower() if countdict.

txt = 'a b c de fgh' arr = txt.split() print('单词数:', len(arr), arr)# 输出结果:# 单词数: 5 ['a', 'b', 'c', 'de', 'fgh']

hhjc.net | 369-e.net | gpfd.net | dfkt.net | lyhk.net | 网站首页 | 网站地图
All rights reserved Powered by www.rprt.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com