python输出最小和第二小的元素位置

Python013

python输出最小和第二小的元素位置,第1张

a=[2,3,6,1,3,1,2]

b=sorted(list(set(a)))

for i,k in enumerate(a):

    if a[i]>b[1]:

        a[i]=0

    elif a[i]==b[1]:

        a[i]=1

    else:

        a[i]=2

如果能用Python现成的函数的话 很简单

我给你提示一下 剩下的应该很简单了

>>> import random

>>> def RandomList(length=10,low=-10,high=10):

...     RandomList=[]

...     for i in range(length):

...             RandomList.append(random.randint(low,high))

...     return RandomList

...

>>> list = RandomList

>>> list

<function RandomList at 0x00000000021F7F98>

>>> list = RandomList()

>>> list

[3, 10, -7, -4, 9, -1, 5, 6, 9, 3]

>>> list=sorted(list)

>>> list

[-7, -4, -1, 3, 3, 5, 6, 9, 9, 10]

>>>

升序排序完成之后 第二小的肯定就是 list[1]而第二大的就是 list[length-2]