12.設有串列lst_odd=[1,3,5,7,9]和串列lst_even=[2,4,6,8,10],試撰寫程式,將兩個串列合并成一個新的串列,并將新串列按照元素的大小降序排列,不改變串列的元素,
lst_odd=[1,3,5,7,9]
lst_even=[2,4,6,8,10]
newlist=lst_odd.copy()
newlist.extend(lst_even)
newlist.sort()
print(newlist)
#輸出結果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
13.撰寫程式,對用戶輸入的英文字串中出現的英文字母進行提取(不區分大小寫,重復字母只記一次),并將提取的結果按照字母表升序排列后輸出,例如,用戶輸入“I miss you.”,程式輸出“i,m,o,s,u,y”或“I,M,O,S,U,Y”,
s=input("請輸入一段英文:")
s=s.lower()
lst=[]
for c in s:
if c.isalpha():
if c not in lst:
lst.append(c)
lst.sort()
print(lst)
#輸出樣例
請輸入一段英文:I miss you
['i', 'm', 'o', 's', 'u', 'y']
14.撰寫程式,生成一年包含20個兩位隨機整數的串列,將其前十個元素升序排列,后十個元素降序排列,
import random
a=range(10,100)
lst=random.sample(a,20)
lst1=sorted(lst[:10])
lst[:10]=lst1
lst2=sorted(lst[10:],reverse=True)
lst[10:]=lst2
print(lst)
#輸出樣例
[12, 17, 33, 49, 51, 60, 61, 69, 81, 90, 79, 66, 64, 59, 55, 53, 43, 39, 30, 22]
15.撰寫程式,將用戶輸入的英文陳述句中的單詞(忽略標點符號)進行逆序排列后輸出,例如:用戶輸入“How are you”,則程式輸出“you are How”,
def sentence_reverse(s):
lst=s.split(" ")
return " ".join(lst[-1::-1])
s1=input("請輸入一段英文:")
s2=sentence_reverse(s1)
print(s2)
#輸出樣例
請輸入一段英文:How are you
you are How
16.假設已有串列lst_floor=[1,4,2,5,7,3],存放了某電梯在一段時間內經過的樓層,試撰寫程式,實作以下功能,
(1)輸出電梯的運行路線(“↑”表示上行一層,“↓”表示下行一層),結果如下:↑↑↑↓↓↑↑↑↑↑↓↓↓↓
lst_floor=[1,4,2,5,7,3]
for i in range(1,len(lst_floor)):
end=lst_floor[i]
start=lst_floor[i-1]
if start<end:
print("↑"*(end-start),end="")
else:
print("↓"*(start-end),end="")
#輸出結果
↑↑↑↓↓↑↑↑↑↑↓↓↓↓
(2)假設運行路線為↑↑↓↓↓↑↑↓↑↑↑↑,且已知初始樓層為2樓,輸出經過的各樓層,結果如下:2,3,4,3,2,1,2,3,2,3,4,5,6
route="↑↑↓↓↓↑↑↓↑↑↑↑"
lst_floor=[2]
floor=2
for ch in route:
if ch=="↑":
floor+=1
else:
floor-=1
lst_floor.append(floor)
print(",".join([str(x) for x in lst_floor]))
#輸出結果
2,3,4,3,2,1,2,3,2,3,4,5,6
17.假設已有串列lst_sides=[3,4,5,6,6,6,4,4,3],依次存放了三個三角形的三條邊長,試撰寫程式,利用海倫公式計算每個三角形的面積,并將結果存入串列lst_area,
lst_sides=[3,4,5,6,6,6,4,4,3]
lst_area=[]
for i in range(0,len(lst_sides),3):
a,b,c=lst_sides[i:i+3]
p=(a+b+c)/2
s=p*(p-a)*(p-b)*(p-c)
lst_area.append(s)
print(lst_area)
#輸出結果
[36.0, 243.0, 30.9375]
18.假設有字串s=“語文:80,數學:82,英語:90,物理:85,化學:88,美術:80”,存放了某個學生的期末成績,試撰寫程式,計算該學生所有科目的總分和平均分(保留一位小數),
s="語文:80,數學:82,英語:90, 物理: 85,化學:88,美術:80"
lst_score=[]
for x in s.split(","):
print(type(x))
print(x)
i=x.index(":")+1
score=int(x[i:])
lst_score.append(score)
zf=sum(lst_score)
print("總分:{}\n平均分:{:.1f}".format(zf,zf/len(lst_score)))
#輸出結果
<class ‘str’>
語文:80
<class ‘str’>
數學:82
<class ‘str’>
英語:90
<class ‘str’>
物理: 85
<class ‘str’>
化學:88
<class ‘str’>
美術:80
總分:505
平均分:84.2
19.假設已有串列lst=[(“triangle”,“shape”),(“red”,“color”),(“square”,“shape”),(“yellow”,“color”),(“green”,“color”),(“circle”,“shape”)],其中每個元素都是一個元組,元組中的每一個元素表示值,第二個元素表示標簽,試撰寫程式,完成以下功能:
(1)將串列lst中的元素按照標簽排序后輸出,
(2)將所有的顏色值從串列lst中提取出來,存入串列lst_colors,并將該串列輸出,
lst=[("triangle","shape"),("red","color"),("square","shape"),("yellow","color"),("green","color"),("circle","shape")]
lst_new=[ [x[1],x[0]] for x in lst ]
print(lst_new)
lst_sort=sorted(lst_new)
print("按照標簽排序后的串列:{}".format(lst_sort))
lst_colors=[x[1] for x in lst_sort if x[0]=="color"]
print("顏色串列:{}".format(lst_colors))
#輸出結果
[[‘shape’, ‘triangle’], [‘color’, ‘red’], [‘shape’, ‘square’], [‘color’, ‘yellow’], [‘color’, ‘green’], [‘shape’, ‘circle’]]
按照標簽排序后的串列:[[‘color’, ‘green’], [‘color’, ‘red’], [‘color’, ‘yellow’], [‘shape’, ‘circle’], [‘shape’, ‘square’], [‘shape’, ‘triangle’]]
顏色串列:[‘green’, ‘red’, ‘yellow’]
20.假設串列lst_suit=[“黑桃”,“紅桃”,“梅花”,“方塊”],存放了撲克牌的花色,串列lst_face=[“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”,“A”,“2”],存放了撲克牌的牌面大小,其元素已按照牌面大小升序排列,試撰寫程式,完成以下功能,
(1)利用串列生成式,將以上兩個串列進行元素搭配,生成一個新的串列,存放所有牌面(不考慮大小王),新串列lst的內容為:[“3黑桃”,“3紅桃”,“3梅花”,“3方塊”,“4黑桃”,“4紅桃”,“4梅花”,“4方塊”,…,“A黑桃”,“A紅桃”,“A梅花”,“A方塊”,“2黑桃”,“2紅桃”,“2梅花”,“2方塊”],
(2)使用random庫的shuffle()函式將串列lst中的元素次序打亂,
(3)用戶與計算機進行“抽牌比大小”游戲,游戲規則如下,
用戶輸入序號(范圍為0~51),程式根據序號在串列lst中讀取牌面,計算機抽牌由程式自動完成(通過random庫中randint()函式隨機生成序號),將用戶抽取的牌面與計算機抽取的牌面進行大小比較(不考慮花色),并將結果輸出,三種輸出內容對照如下:“恭喜,您輸了!” “很遺憾,您輸了” “咱們平手了!”
import random
lst_suit=["黑桃","紅桃","梅花","方塊"]
lst_face=["3","4","5","6","7","8","9","10","J","Q","K","A","2"]
lst=[x+y for x in lst_face for y in lst_suit]
random.shuffle(lst)
idx_user = int(input("請您抽一張牌(0~51):"))
card_user=lst[idx_user]
print("您抽到的牌是:{}".format(card_user))
idx_computer=random.randint(0,51)
card_computer=lst[idx_computer]
print("電腦抽到的牌是:{}".format(card_computer))
val_user=lst_face.index(card_user[0])
val_computer=lst_face.index(card_computer[0])
if val_user>val_computer:
print("恭喜,您贏了!")
elif val_user<val_computer:
print("很遺憾,您輸了!")
else:
print("咱們平手了!")
#輸出樣例
請您抽一張牌(0~51):8
您抽到的牌是:7紅桃
電腦抽到的牌是:A梅花
很遺憾,您輸了!
21.學校舉辦朗讀比賽,邀請了10位評委為每一名參賽選手的表現打分,假設串列lst_score=[9,10,8,9,10,7,6,8,7,8],存放了某一位參賽選手的所有評委打分,試撰寫程式,根據以下規則計算該參賽選手的最終得分:
(1)去掉一個最高分
(2)去掉一個最低分
(3)最終得分為剩下8和分數的平均值
lst_score=[9, 10, 8, 9, 10, 7, 6, 8, 7, 8]
lst_score.sort()
lst_score.pop()
lst_score.pop(0)
print("該選手的最終得分為{}".format(sum(lst_score)/len(lst_score)))
#輸出結果
該選手的最終得分為8.25
22.假設串列lst_weather中存放了一周的天氣情況(包括最低氣溫、最高氣溫、天氣狀況、風力和空氣質量質量等級),試撰寫程式,統計一下資料:
(1)空氣質量為優的天數
(2)風力低于3級且最高氣溫不超過25℃的天數
(3)平均氣溫低于20℃的天數
測驗資料:lst_weather=[[“周一”,“16℃”,“26℃”,“多云”,“1級”,“優”],[“周二”,“17℃”,“27℃”,“晴”,“2級”,“優”],[“周三”,“16℃”,“28℃”,“晴”,“3級”,“優”],[“周四”,“16℃”,“25℃”,“陰”,“2級”,“良”],[“周五”,“15℃”,“24℃”,“陰”,“2級”,“良”],[“周六”,“15℃”,“25℃”,“晴”,“3級”,“優”],[“周日”,“14℃”,“23℃”,“小雨”,“3級”,“良”]]
lst_weather=[["周一", "16℃", "26℃","多云","1級","優"],[ "周二" ,"17℃", "27℃","晴","2級","優"],[ "周三","16℃", "28℃","晴","3級","優"],[ "周四","16℃", "25℃","陰","2級","良"],[ "周五","15℃", "24℃","陰","2級","良"],[ "周六", "15℃", "25℃","晴","3級","優"],[ "周日","14℃", "23℃","小雨","3級","良"]]
number_condition1=[x[0] for x in lst_weather if x[5]=="優"]
print("空氣質量為優的天數:{},它們分別是:{}".format(len(number_condition1),",".join(number_condition1)))
number_condition2=[x[0] for x in lst_weather if (int(x[2][:-1])<=25 and int(x[4][:-1])<3)==True]
print("風力低于3級且最高氣溫不超過25℃的天數:{},它們分別是:{}".format(len(number_condition2),",".join(number_condition2)))
number_condition3=[x[0] for x in lst_weather if (int(x[1][:-1]) + int(x[2][:-1]))/2<20]
print("平均氣溫低于20℃的天數:{},它們分別是:{}".format(len(number_condition3),",".join(number_condition3)))
#輸出結果
空氣質量為優的天數:4,它們分別是:周一,周二,周三,周六
風力低于3級且最高氣溫不超過25℃的天數:2,它們分別是:周四,周五
平均氣溫低于20℃的天數:2,它們分別是:周五,周日
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279268.html
標籤:python
上一篇:python爬取Json資料,re提取,存盤Json資料,進行評論分析(某奇藝斗破蒼穹評論)
下一篇:一篇文章讓你快速入門Pandas
