python怎么生成随机不重复数组

python生成随机不重复数组的方法:可以利用python中的randomw.sample()函数来实现。sample(x,y)函数的作用是从序列x中,随机选择y个不重复的元素。

python怎么生成随机不重复数组

利用python中的randomw.sample()函数

(推荐教程:python入门教程)

resultList=random.sample(range(A,B),N)

sample(x,y)函数的作用是从序列x中,随机选择y个不重复的元素。表示从[A,B)间随机生成N个数,结果以列表返回123。

注意:区间为[A,B) 左闭右开

>>>random.sample(range(1,5),3)
[2,4,3]