python中利用循環結構求最大值、最小值、統計元素次數


 

1、求最大值和最小值

>>> test1 = [34,2,5,65,322,21,54] >>> temp = test1[0] >>> for i in test1: if i > temp: temp=i >>> temp 322
>>> for i in test1: if i < temp: temp=i >>> temp 2

 

2、統計元素次數

>>> test1 = ["a","e","a","b","e","a","b","e"] >>> temp=test1[0] >>> temp 'a'
>>> test2=[] >>> for i in test1: if temp == i: test2.append(i) >>> len(test2) 3

 


免責聲明!

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



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