python - 如何从 Python 列表中弹出多个随机元素?
问题描述
每当我从中采样时,我都会尝试从原始列表中删除元素。
list_a = ["a", "b", "c", "d"]
list_b = np.random.choice(list_a, 2)
当我np.random.choice
,我想list_a
成为一个没有元素的列表list_b
。
解决方案
这可以使用 python 的 remove() 来完成。
您可以在此处阅读有关 remove() 的更多信息:https ://www.programiz.com/python-programming/methods/list/remove
import numpy as np
list_a = ["a", "b", "c", "d"]
list_b = np.random.choice(list_a, 2)
for i in list_b:
list_a.remove(i)
print list_a
print list_b
结果:
list_a = [a, c]
list_b = [b, d]
推荐阅读
- validation - Symfony 3.3 如何为两个嵌套的 collectionType 添加约束
- session - 会话关闭后 Tensorflow 不释放内存
- json - how to display my JSON object in good format in VBA Excel
- google-analytics - Google Analytics APIs - How can I obtain landing page path list?
- google-workspace - Gmail addon Inline installation
- javascript - Action Script 3 - ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller
- java - Java 报告 Java 版本已被弃用,即使安装了最新版本
- hyperledger-fabric - 恐慌:没有为fabric-ca库设置版本
- java - 无法使用 Spring Security 配置访问静态文件夹中的内容
- npm - 如何指示`npm`不下载现有软件包?