1.统计字符串字符个数,空格字符个数,数字字符个数,其他字符个数 1星
# -*- coding: utf-8 -*-
s = input("please input string:")
print(s)
letters = 0
space = 0
digit = 0
others = 0
for ch in s:
#是否为字母
if ch.isalpha():
letters +=1
#是否为空格
elif ch.isspace():
space +=1
#是否为数字
elif ch.isdigit():
digit += 1
else:
others += 1
print(letters, space, digit, others)
PS:在输入时如果不想带“”,可以用raw_input(),它将所有输入视为字符串。
2.把每个合数分解成几个质数相乘的形式,例如:100=225*5,并把代码封装成函数的形式。`````
# -*- coding: utf-8 -*-
def f(n):
result = []
i = 2
str1 = str(n) + '='
while(n>1):
if(n%i==0):
n/=i
result.append(str(i))
i -= 1
i += 1
str1 += '*'.join(result)
return str1
if __name__ == '__main__':
for i in range(100,120):
print(f(i))
3.编写函数,获得目录下所有的文件名。
# -*- coding: utf-8 -*-
import os
#要检查的文件类型
dict_filetype = ["cpp", "c", "h"]
#brief:递归获得所有文件名
#param:path起始目录,要检查的根目录
#param:allfile天空即可
#result:列表所有与dictionary——filetype对应的文件名
def get_filename(path, allfile):
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
#判断文件夹
if(os.path.isdir(filepath)):
#文件夹递归
get_filename(filepath,allfile)
else:
#文件,判断文件类型
for filetype in dict_filetype:
temp_file_type = filepath.split(".")
if(filetype==temp_file_type[-1]):
allfile.append(filepath)
break
else:
print("the file is not code:%s"%filepath)
return allfile
if __name__ == '__main__':
allfile=[]
get_filename("/home/wc/test", allfile)
for tmp in allfile:
print(tmp)
输出:
the file is not code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/graph1.png the file is not
code:/home/wc/test/graph1.png the file is not
code:/home/wc/test/graph1.png /home/wc/test/test.cpp