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

题目:

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

>>> 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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值