python 列表随机抽样
时间: 2023-09-03 21:14:43 浏览: 139
在Python中,可以使用random模块中的sample函数来从给定列表中进行随机抽样。这个函数接受两个参数,第一个参数是要进行抽样的列表,第二个参数是要抽样的样本数量。样本数量可以根据需求进行设置。下面是一个示例代码:
```
import random
list1 = ['a', 'b', 'c', 'd', 'e']
ns = 2
samples = random.sample(list1, ns)
print(samples)
```
这段代码会从list1中随机选择2个样本,并将结果打印出来。例如,可能的输出是['a', 'c']。 https://towardsdatascience.com/mastering-random-sampling-in-python-ac2df84b7d3f
翻译自: https://towardsdatascience.com/mastering-random-sampling-in-python-ac2df84b7d3f<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [如何用Python实现从列表中随机取样](https://blog.csdn.net/Eric005/article/details/113834960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python随机抽样_掌握python中的随机抽样](https://blog.csdn.net/weixin_26736149/article/details/108915780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文