Python: how to find value in list smaller than target

后端 未结 6 1191
一个人的身影
一个人的身影 2021-01-12 08:51

For example I have a non-ordered list of values [10, 20, 50, 200, 100, 300, 250, 150]

I have this code which returns the next greater value:

def GetN         


        
6条回答
  •  -上瘾入骨i
    2021-01-12 09:32

    a=[4,3,8,2,5]
    temp=4
    def getSmaller(temp,alist):
        alist.sort()
        for i in range(len(alist)):
            if(i>0 and alist[i]==temp):
                print alist[i-1]
            elif(i==0 and alist[i]==temp):
                print alist[i]
    getSmaller(temp,a)
    

提交回复
热议问题