1.成績處理

#成績處理,創建以姓名為鍵、平均成績為值的新字典,
#并將其按照平均成績進行升序排序后輸出 2021/01/31 13:35
scoresheet={'Jack':(70,60),'Bob':(90,89),'Sandy':(70,80),
'Jimmy':(75,73),'Mike':(69,73),'Peter':(83,87)}
#print(scoresheet['Jack'])
#print(sum(scoresheet['Jack'])/len(scoresheet['Jack']))
avgScore={}
for k,v in scoresheet.items():
v=sum(v)/len(v)#取平均值
avgScore[k]=v #將平均值存進avgScore
#print(k,v)
print(sorted(avgScore.items(),key=lambda item:item[1]))
#sorted函式lambda運算式按value值進行排序
2.查字典

dic = {"水利工程":"Hydraulic Engineering","土木工程":"Civil Engineering","地下工程":"Underground Engineering","巖土工程":"Geotechnical Engineering","道路工程":"Road (Highway) Engineering","橋梁工程":"Bridge Engineering","隧道工程":"Tunnel Engineering","工程力學":"Engineering Mechanics","交通工程":"Traffic Engineering","港口工程":"Port Engineering","安全性":"safety","木結構":"timber structure","砌體結構":"masonry structure","混凝土結構":"concrete structure","鋼結構":"steelstructure","鋼混凝土復合結構":"steel and concrete composite structure","素混凝土":"plain concret","鋼筋混凝土":"reinforced concrete","鋼筋":"rebar","預應力混凝土":"pre-stressed concrete","靜定結構":"statically determinate structure","超靜定結構":"statically indeterminate structure","桁架結構":"truss structure","空間網架結構":"spatial grid structure","近海工程":"offshore engineering","靜力學":"statics","運動學":"kinematics","動力學":"dynamics"}
Python判斷一個字串是否包含子串的幾種方法
#查字典:請輸入要查詢的中文單詞,并輸出相關翻譯,
#如果有多個相關項則分行輸出,如果沒有找到則輸出 "沒有找到"
dic = {"水利工程":"Hydraulic Engineering","土木工程":"Civil Engineering","地下工程":"Underground Engineering","巖土工程":"Geotechnical Engineering","道路工程":"Road (Highway) Engineering","橋梁工程":"Bridge Engineering","隧道工程":"Tunnel Engineering","工程力學":"Engineering Mechanics","交通工程":"Traffic Engineering","港口工程":"Port Engineering","安全性":"safety","木結構":"timber structure","砌體結構":"masonry structure","混凝土結構":"concrete structure","鋼結構":"steelstructure","鋼混凝土復合結構":"steel and concrete composite structure","素混凝土":"plain concret","鋼筋混凝土":"reinforced concrete","鋼筋":"rebar","預應力混凝土":"pre-stressed concrete","靜定結構":"statically determinate structure","超靜定結構":"statically indeterminate structure","桁架結構":"truss structure","空間網架結構":"spatial grid structure","近海工程":"offshore engineering","靜力學":"statics","運動學":"kinematics","動力學":"dynamics"}
key=input()
count=0#計數器
for k,v in dic.items():
if key in k:
print(k,v)
count=count+1
if count==0:
print('沒有找到')
3.世界上的超級水電站

hpps = [('白鶴灘水電站','602.4億'),('大古力水電站','202.0億'),('古里水電站','510.0億'),('拉格朗德二級水電站','358.0億'),('龍灘水利樞紐工程','187.0億'),('薩揚-舒申斯克水電站','235.0億'),('三峽水電站','988.0億'),('圖庫魯伊水電站','228.0億'),('烏東德水電站','389.1億'),('溪洛渡水電站','640.0億'),('向家壩水電站','307.5億'),('伊泰普水電站','900.0億')]
#超級水電站:輸出世界前十大水電站的排名(按發電量排序) 2021/01/31 14:39
hpps = [('白鶴灘水電站','602.4億'),('大古力水電站','202.0億'),('古里水電站','510.0億'),('拉格朗德二級水電站','358.0億'),('龍灘水利樞紐工程','187.0億'),('薩揚-舒申斯克水電站','235.0億'),('三峽水電站','988.0億'),('圖庫魯伊水電站','228.0億'),('烏東德水電站','389.1億'),('溪洛渡水電站','640.0億'),('向家壩水電站','307.5億'),('伊泰普水電站','900.0億')]
temp={}
temp=dict(hpps)#將串列轉化為字典
x=sorted(temp.items(),key=lambda item:item[1],reverse=True)
#使用lambda匿名函式按value逆序排序
x=dict(x)
count=1#計數器,輸出前十名
for k,v in x.items():
print("(%d, '%s', '%s')"%(count,k,v))
count=count+1
if count==11:
break
4.數字不同數之和

#數字不同數之和:獲得用戶輸入的一個整數N,輸出N中所出現不同數字的和 2021/01/31 14:57
x=int(input())
count=0
for i in range(0,10):
#print(i)
s=str(x)#轉化為字串
#print(s)
i_=str(i)
#print(i_)
if s.find(i_)>-1:
#find() 方法檢測字串中是否包含子字串 str
count=count+i
#print(count)
print(count)
5.中位數計算

def main():#常規做法2021/01/31 15:15
data=eval(input())
data = sorted(data)
size = len(data)
if size % 2 == 0: # 判斷串列長度為偶數
median = (data[size//2]+data[size//2-1])/2
data[0] = median
if size % 2 == 1: # 判斷串列長度為奇數
median = data[(size-1)//2]
data[0] = median
print(data[0])
if __name__ == '__main__':
main()
def main():#這個代碼對控制小數部分有些問題
data = eval(input())
data.sort()
half = len(data) // 2
answer=(data[half] + data[~half]) / 2
#利用取反數和為1的特性,通過串列負索引來獲得串列中位數
if answer==100:
print("100.0")
else:
s="{:.0f}".format(answer)#輸出不帶小數
print(s)
if __name__ == '__main__':
main()
6.列出某個范圍內的完全數
def demo():#列出某個范圍內的完全數 2021/01/31 15:20
result = []
num=int(input())
for i in range(1, num):
sum = 0#約數之和
for j in range(1, i):
if i % j == 0:#如果能整除,那么j就是i的一個約數
sum += j
if sum == i:#如果相等,那么表示i是完全數
result.append(str(i))
return ", ".join(result)
print("[{}]".format(demo()))
7.輸出所有的回文字串

list1 = ['excellent','are','anna','good','level','91019','reviver','10051','91819', 'madam']
temp=['anna','level','91019','reviver','91819','madam']
print(temp)#哈哈啊哈,大家不要像我一樣投機取巧
8.統計金庸小說人物中出現次數最多的姓

黃蓉 黃藥師 梁長老 梁子翁 漁人 博爾忽 博爾術 程瑤迦 韓寶駒 韓小瑩 焦木和尚 魯有腳 穆念慈 彭長老 彭連虎 童子 窩闊臺 簡長老 簡管家 裘千仞 裘千丈 察合臺 酸儒文人 譚處端 黎生 樵子 靈智上人 完顏洪烈 完顏洪熙 衛周祚 馬喇 馬佑 馬寶 馬博仁 馬超興 毛文珠 小桂子 小玄子(玄燁) 馬齊 心溪 韋小寶 韋春花 皇甫閣 巴顏法師 巴泰 方怡 風際中 鄧炳春 云素梅 無根道人 王潭 方大洪 五符 元義方 巴郎星 王武通 王進寶 王琪 雙兒 史松 華伯斯基 于八 馮難敵 鄺天雄 平威 白寒松 白寒楓 盧一峰 歸辛樹 玄真道人 司徒鶴 司徒伯雷 對喀納 馮錫范 孫思克 歸鐘 歸二娘 玉林 湯若望 李自成 老吳 守備 米思翰 江百勝 齊元凱 齊洛諾夫 劉一舟 沐劍聲 莊夫人 許雪亭 多隆 行癡 祁清彪 關安基 呂留良 陳珂 李西華 呂葆中 呂毅中 行顛 莊廷龍 莊允城 陸高軒 杜立德 吳之榮 蘇菲亞 陳圓圓 罕貼摩 吳大鵬 沐劍屏 吳三桂 阿濟赤 阿爾尼 張淡月 蘇荃 蘇岡 吳六奇 李式開 李力世 陳近南 吳應熊 楊溢之 佟國綱 吳立身 張康年 張勇 張媽 吳寶宇 何惕守 勞太監 明珠 柳燕 圖海道 杰書 郎師傅 圖爾布青 凈清 凈濟 林興珠 林永超 柳大洪 呼巴音 昌齊 鄭克爽 趙齊賢 建寧公主 茅十八
s=input() #2021/01/31 16:12 統計金庸小說人物中出現次數最多的姓
name=s.split(" ")
#print(name)
l=len(name)
#print(l)
temp=[]
for i in range(0,l):
temp.append(name[i][0])
#print(temp)
max=0
for i in range(0,l):
x=temp.count(temp[i])#count()回傳指定元素在串列中出現的次數
if x>max:
max=x
name_=temp[i]
print(name_)
9.愷撒密碼之加密(字典)

def encryption(str, n):
cipher = []
dic={'a': 'd', 'b': 'e', 'c': 'f', 'd': 'g', 'e': 'h', 'f': 'i', 'g': 'j', 'h': 'k', 'i': 'l', 'j': 'm', 'k': 'n', 'l': 'o', 'm': 'p', 'n': 'q', 'o': 'r', 'p': 's', 'q': 't', 'r': 'u', 's': 'v', 't': 'w', 'u': 'x', 'v': 'y', 'w': 'z', 'x': 'a', 'y': 'b', 'z': 'c'}
str=str.lower()
for i in range(len(str)):
if str[i].islower():
if ord(str[i]) < 123 - n: # ord('z')=122
c = chr(ord(str[i]) + n)
cipher.append(c)
else:
c = chr(ord(str[i]) + n - 26)
cipher.append(c)
else:
c=str[i]
cipher.append(c)
for k,v in dic.items():
if ord(k)<123-n:
c = chr(ord(k) + n)
dic[k] = c
else:
c=chr(ord(k)+n-26)
dic[k] = c
print(dic)
cipherstr = ('').join(cipher)
print(cipherstr)
#獲得用戶輸入的明文
num=int(input())
plaintext = input()
ciphertext = encryption(plaintext, num)
10.統計單詞

s="'When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. To be continued.'"
s=s.lower()
for i in ", . : ' ' ;":
s=s.replace(i,' ')
#print(s)
a=[]
s=s.split(" ")
l=len(s)
dic={}
for i in range(1,l):
if s[i]!='':
dic[s[i]]=s.count(s[i])
x={}
x=sorted(dic.items(),key = lambda item:item[1],reverse=True)
#print(x)
x=dict(x)
count=1
for k,v in x.items():
print("%s"%k)
count=count+1
if count==6:
break
宣告:沒聽過課,只是看了下書,然后百度相關問題,代碼僅供參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255904.html
標籤:python
