defcmul(a,*b):for i in b:
a*=i
return a
print(eval("cmul({})".format(input())))
斐波那契数列
deffbi(n):if n ==1or n ==2:return1else:return fbi(n-1)+fbi(n-2)
n =eval(input())print(fbi(n))
汉诺塔实践
steps =0defhanoi(src, des, mid, n):global steps
if n ==1:
steps+=1print("[STEP{:>4}] {}->{}".format(steps, src, des))else:
hanoi(src,mid,des,n-1)
steps+=1print("[STEP{:>4}] {}->{}".format(steps, src, des))
hanoi(mid,des,src,n-1)
N =eval(input())
hanoi("A","C","B", N)
基本统计值计算
#请在...补充一行或多行代码#CalStatisticsV1.pydefgetNum():#获取用户不定长度的输入
getNums =input()
s= getNums.split(",")for i inrange(len(s)):
s[i]=eval(s[i])return s
defmean(numbers):#计算平均值#numbers 是一个列表
res =0for i inrange(len(numbers)):
res += numbers[i]return res /len(numbers)defdev(numbers, mean):#计算标准差
sdev =0.0for num in numbers:
sdev = sdev +(num - mean)**2returnpow(sdev /(len(numbers)-1),0.5)defmedian(numbers):#计算中位数#进行排序sorted(numbers)
size =len(numbers)if size %2==0:return(numbers[size//2-1]+ numbers[size //2])/2else:return numbers[size//2]
n = getNum()#主体函数
m = mean(n)print("平均值:{:.2f},标准差:{:.2f},中位数:{}".format(m,dev(n,m),median(n)))
文本词频统计:引文版哈姆雷特
defgetText():
txt =open("hamlet.txt","r").read()#取出大小写影响
txt = txt.lower()#将特殊字符 用空格替换for ch in'!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch," ")return txt
hamletTxt = getText()#分割为一个个的词 ,返回的是一个列表
words = hamletTxt.split()#使用字典保存 词-》出现次数的 映射
counts ={}#遍历列表,统计词以及对应的次数for word in words:
counts[word]= counts.get(word,0)+1#将字典转为列表进行排序
items =list(counts.items())#排序
items.sort(key=lambda x:x[1], reverse =True)#取前十个for i inrange(10):print(items[i][0])
s =input()try:
d =eval(s)
res ={}for k in d:
res[d[k]]= k
print(res)except:print("输入错误")
《沉默的羔羊》之最多单词
import jieba
f =open("沉默的羔羊.txt")
ls = jieba.lcut(f.read())#ls = f.read().split()
d ={}for w in ls:iflen(w)>2:
d[w]= d.get(w,0)+1
maxc =0
maxw =""#遍历字典,找到最大的词频for k in d:if d[k]> maxc:
maxc = d[k]
maxw = k
elif d[k]== maxc and k > maxw:
maxw = k
print(maxw)
f.close()
文件行数
f =open("latex.log","r")
count =0for line in f:
line = line.strip("\n")if line =="":continue
count+=1print("共{}行".format(count))
文件字符分布
f =open("latex.log","r")#只扫描a -z所以要对字典进行初始化#定义一个字典统计 单词与出现次数的映射
d ={}#初始化for i inrange(26):
d[chr(ord("a")+i)]=0#计算字符次数
count =0#嵌套遍历for line in f:for c in line:
d[chr(ord(c))]= d.get(chr(ord(c)),0)+1
count+=1print("共{}字符".format(count),end="")#最后只遍历26个字母for i inrange(26):
word,count =(chr(ord("a")+i)),d.get(chr(ord("a")+i))print(",{}:{}".format(word,count),end="")
文件独特行数
f =open("latex.log")#先读取全部行,返回的是一个列表,每一行代表一个元素
lines = f.readlines()#先进行一次去重
s =set(lines)#将不重复的行进行删除for i in s:
lines.remove(i)#剩下的就是重复的行#对重复的行进行去重,
t =set(lines)#最终的结果 = 不重复的行 - 重复的行 print("共{}独特行".format(len(s)-len(t)))
CSV格式列变换:
f =open("data.csv")
ls =[]for line in f:
line = line.replace("\n","")
ls.append(line.split(","))
f.close()#每一行进行逆置for row in ls:print(",".join(row[::-1]))
CSV格式数据清洗
f =open("data.csv")#读取每一行,然后使用 , 进行分隔 返回一个列表for line in f:#先将当前行的换行符去除
line = line.strip("\n")#将 , 替换为,
ls = line.replace(", ",",")#输出print(ls)
f.close()
七段数码管绘制import turtle as timport timedef gap(): t.penup() t.fd(5)def drawline(draw): gap() t.pendown() if draw else t.penup() t.fd(40) gap() t.right(90) def drawDigit(n): drawline(True) if n in [2,3,4,5,6,8,9] el