list 求和 平均值 最大值 ---reverse 源列表反轉 ---sort 正序排序
阿新 • • 發佈:2020-07-10
# 求和 l = [88,97,79,89,76] re = 0 for i in l: re += 1 print(re//5) # 平均值 # print(sum(l)) # he print(max(l)) # max # max 推演思路 #預設第一個值最大 打擂 l[0] # 後邊的元素和參照物依次作比較迴圈 # 後邊元素大於參照物 重新賦值給參照物 l = [88,97,79,89,76] max_num = l[0] # 參照物 i = 1 # 不用和本身比較 和 後一位比較 while i < len(l):if l[i]> max_num: max_num = l[i] i += 1 print(max_num) l = [88,97,79,89,76] xiao_num = l[0] i = 1 while i < len(l): if l[i] < xiao_num: xiao_num = l[i] i += 1 print(xiao_num) # reverse 源列表反轉 # sort 正序排序 l = [88,97,79,89,76] l.reverse() print(l) # [76, 89, 79, 97, 88]# 反序推演過程 l = [88,97,79,89,76] i = len(l)- 1 while i >=0: print(l[i],end=' ') # + end=' ' 結束預設換行 i -= 1 # 76, 89, 79, 97, 88 # 正序推演過程 i = 0 while i < len(l): print(l[i]) i += 1 # 88,97,79,89,76
# 求和
l = [88,97,79,89,76]
re = 0
for i in l:
re += 1
print(re//5) # 平均值
#(sum(l)) # he
print(max(l)) # max
# max 推演思路
#預設第一個值最大 打擂 l[0]
# 後邊的元素和參照物依次作比較迴圈
# 後邊元素大於參照物 重新賦值給參照物
l = [88,97,79,89,76]
max_num = l[0] # 參照物
i = 1 # 不用和本身比較 和 後一位比較
while i < len(l):
if l[i]> max_num:
max_num = l[i]
i += 1
print(max_num)
l = [88,97,79,89,76]
xiao_num = l[0]
i = 1
while i < len(l):
if l[i] < xiao_num:
xiao_num = l[i]
i += 1
print(xiao_num)
#
# reverse 源列表反轉
# sort 正序排序
l = [88,97,79,89,76]
l.reverse()
print(l) # [76, 89, 79, 97, 88]
# 反序
l = [88,97,79,89,76]
i = len(l)- 1
while i >=0:
print(l[i],end=' ') # + end=' ' 結束預設換行
i -= 1
# 76, 89, 79, 97, 88
# # 正序
i = 0
while i < len(l):
print(l[i])
i += 1
# 88,97,79,89,76