python

您所在的位置:网站首页 python随机整数列表 python

python

2022-05-05 07:39| 来源: 网络整理| 查看: 265

标签:white   14.   分享   fetch   .so   div   列表   height   random   

1.  列表排序可采用两种方法

sorted(list) 直接改变list

调用list的方法 list.sort

 

2.

random.randint(a,b) 生成大于等于a小于等于b的整数

random.random() 生成一个在[0,1)区间上的实数

random.choice(sequence) sequence泛指list、tuple、字符串等

random.randrange(start,stop,step) step务必给出,在本次实践中,如果不给出将会随机给一个值且极大可能为负数

技术分享图片

 

具体参考 https://www.cnblogs.com/whiteprism/p/6290814.html

3. 代码实例

导入模块 import random

def FetchRandom(): ‘生成随机数列表并从中抓取‘ N1 = random.randint(2,100) N2 = random.randint(1,100) while N2>N1: N2 = random.randint(1,100) alist = [] for i in range(N1): n = random.randint(0, 2 ^ 31 - 1) alist.append(n) blist = [] for i in range(N2): blist.append(random.randrange(alist[0],alist[N1-1],1))     #blist.append(random.choice(alist))  print sorted(blist) print ‘N1=‘,N1,‘ N2=‘,N2,‘ n=‘,n

 

4. 输出效果

 技术分享图片

 

python_生成随机数与列表排序

标签:white   14.   分享   fetch   .so   div   列表   height   random   

原文地址:https://www.cnblogs.com/AHappyBird/p/9340841.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3