ITPub博客

首页 > IT职业 > IT职场 > 从列表中或数组中随机抽取固定数量的元素组成新的数组或列表

从列表中或数组中随机抽取固定数量的元素组成新的数组或列表

IT职场 作者:sunoct 时间:2021-09-09 09:43:28 0 删除 编辑

从列表中或数组中随机抽取固定数量的元素组成新的数组或列表

1:python版本:python里面一行代码就能随机选择3个样本
```
>>> import random
>>> mylist=list(range(1,10))
>>> mylist
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> newlist = random.sample(mylist, 3)  #从mylist中随机获取3个元素
>>> newlist
[4, 7, 2]
>>> newlist = random.sample(mylist, 3)  #从mylist中随机获取3个元素
>>> newlist
[4, 3, 1]
>>> newlist = random.sample(mylist, 3)  #从mylist中随机获取3个元素
>>> newlist
[5, 9, 3]
>>>
```

2:jQuery版本
那么jQuery中怎么随机选出固定数组数组[1, 2, 3, 4, 5, 6, 7, 8, 9]中的三个元素,并构造成新数组的?
```


    


        
        
        
    
    
        
    
    

```
> 原文作者:祈澈姑娘
> 技术博客:[https://](http://link.zhihu.com/?target=https%3A//)
>
> 90后前端妹子,爱编程,爱运营,爱折腾。 坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2280/viewspace-2806535/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    83
  • 访问量
    810