本文共 2393 字,大约阅读时间需要 7 分钟。
设计一个函数,统计任意一串字符串中数字字符的个数
def numCount():count = 0str = raw_input('please input something:')for i in str: if i.isdigit(): count+=1print count
numCount()
2,设计函数,统计任意一串字符串中每个字母的个数,不区分大小写
import string
def lettersCount():
list1=[]str = raw_input('please input something:')for i in str: if i.isalpha(): list1.append(i.lower())for j in string.lowercase: n = list1.count(j) if not n: continue else: print '%s 的个数为%s' %(j,n)
lettersCount()
2018-01-02 13:16 添加评论 评分
0agh353272297
1、设计一个函数,统计任意一串字符串中数字字符的个数
#!/usr/bin/env python
str1 = raw_input("请输入字符: ")
def iCount(s):
Count = 0for i in s: if i.isdigit(): Count += 1 else: passprint "数字=%d" % (Count)
if name == 'main':
iCount(str)
2、设计函数,统计任意一串字符串中每个字母的个数,不区分大小写
#!/usr/bin/env python
str1 = raw_input("请输入字符: ")
def strCount(s):
s =list( ''.join(s.split()))s1 = set(s)l = list(s1)t = {}for i in range(len(l)): num = 0 for j in reversed(range(len(s))): if l == s[j]: num = num + 1 s.pop(j) t[l] = str(num) return t
if name == 'main':
t = strCount(str1)print(t)
2018-01-02 17:38 添加评论 评分
0LINUX_A - 不要在最能吃苦的年龄选择安逸!
#!/usr/bin/env python
list1 = []
aa = raw_input("pls input sth: ")
for i in aa:
if i in "0123456789": list1.append(i)
print "the input number is {}".format(len(list1))
list1 = []
list2 = {}
aa = raw_input("pls input sth: ")
for i in aa:
if i.isalpha(): list1.append(i)
for i in list1:
list2 = list1.count(i)
for k,v in list2.iteritems():
print " total this character {} is {}".format(k,v)
2018-01-03 15:56 添加评论 评分
0zhouyuyao
设计一个函数,统计任意一串字符串中数字字符的个数
例如:
"adfdfjv1jl;2jlk1j2" 数字个数为3个
def countNum():
co = 0s = input("Please input a string : ")for c in s: if c.isdigit(): co+=1print("The string have {} number.".format(co))
countNum()
设计函数,统计任意一串字符串中每个字母的个数,不区分大小写
例如:
"aaabbbcccaae111"
a 5个
b 3个
c 3个
e 1个
import re
def countNum():
a={}# s1=''s = input("Please input a string : ")s1=re.sub('[^a-zA-Z]','',s)a = {i: s.count(i) for i in s1}print(a)
countNum()
转载于:https://blog.51cto.com/goldstar52/2136285