PTA的Python練習題(十二)-第4章-7 統計學生平均成績與及格人數


第4章-7 統計學生平均成績與及格人數

a=eval(input())
b=list(map(int,input().split()))
sum=sum(b)
c=[i for i in b if i>=60]
count=len(c)
print('average = %.1f'%(sum/a))
print('count = %d'%(count))

如果這里用append輸入只能實現一行一行輸入,不能實現同一行輸入

知識點(列表的操作):

 """ ①實現嵌套列表的平鋪 """ vec = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] """ 第一個循環為外循環,第二個循環為內循環 """ print([num for elem in vec for num in elem]) print(list(chain(*vec))) """ 過濾不符合條件的元素 """ alist = [-1, -4, 6, 7.5, -2.3, 9, -11] print([i for i in alist if i > 0]) scores={"Zhang San":45, "Li Si":78, "Wang Wu":40, "Zhou Liu":96, "Zhao Qi":65, "Sun Ba":90, "Zheng Jiu":78, "Wu Shi":99, "Dong Shiyi":60} highest = max(scores.values()) lowest = min(scores.values()) average = sum(scores.values())/len(scores) print((highest, lowest, average)) highestPerson = [name for name, score in scores.items() if score == highest] print(highestPerson) from random import randint x = [randint(1, 10) for i in range(20)] print(x) m = max(x) print([index for index, value in enumerate(x) if value == m]) """ 同時遍歷多個列表或可迭代對象 """ print([(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]) print([(x, y) for x in [1, 2, 3] if x == 1 for y in [3, 1, 4] if y != x]) """ 使用列表解析式實現矩陣轉置 """ matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print([[row[i] for row in matrix] for i in range(4)]) print(list(map(list, zip(*matrix))))

 參考資料https://www.jianshu.com/p/da6e449f4c6a


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM