Python找出列表中的最大數和最小數


Python找出列表中數字的最大值和最小值

思路:

先使用冒泡排序將列表中的數字從小到大依次排序

取出數組首元素和尾元素

運行結果:

 

 源代碼:

 1 '''
 2  4.編寫函數,功能:找出多個數中的最大值與最小值。  3 '''
 4 def findNumValue(list=[]):  5     for i in range(len(list)-1):  6         for j in range(len(list)-1-i):  7             if list[j]>list[j+1]:  8                 temp=list[j]  9                 list[j]=list[j+1] 10                 list[j+1]=temp 11     print("最小值:{0}".format(list[0])) 12     print("最大值:{0}".format(list[len(list)-1]))


免責聲明!

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



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