Python请定义函数 count(str,c),统计字符串 str中单个字符 c出现的次数,并设? python中如何将字符串 str 中出现的所有字符 c 删...

Python \u8bf7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570count(s, c),\u5b83\u68c0\u67e5\u5b57\u7b26\u4e32s\u4e2d\u5355\u4e2a\u5b57\u7b26c\u51fa\u73b0\u7684\u6b21\u6570\uff1f

import string
def chartype(ch):
if ch in string.ascii_letters: return 'ascii_letters'
elif ch in string.digits: return 'digits'
elif ch in string.whitespace: return 'whitespace'
else: return 'other'

def iterchtypecount(s):
counter = {}
for c in s:
counter.setdefault(chartype(c), []).append(c)
for t, lst in counter.items():
yield t, len(lst)

for chtype, cnts in iterchtypecount(raw_input("Enter a string: ")):
print chtype, cnts

s="cabcdefc"
s.replace('c','')

def count(str,c):
flag=0
for i in str:
if i==c:
flag+=1
return flag

扩展阅读:python for in count ... python编程 ... python的index用法 ... python str count ... python map函数输入 ... python如何自己定义函数 ... python elif 用法 ... python row函数的使用方法 ... eval在python中的用法 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网