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

 

posted @ 2020-12-26 13:52  小鲨鱼2018  阅读(3712)  评论(0编辑  收藏  举报