作业帮 > 综合 > 作业

python 在随机数列中找第二大和第二小的数字

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/15 02:01:44
python 在随机数列中找第二大和第二小的数字
import random
def RandomList(length=10,low=-10,high=10):
RandomList=[]
for i in range(length):
RandomList.append(random.randint(low,high))
这是现在已有的.急
如果能用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]