python - 来自两个列表的随机元素 - Python
问题描述
所以我在 Python 中有两个列表:
import random
list_1 = ['1','2','3']
list_2 = ['4','5','6']
num = random.choice(list_1 or list_2)
这似乎不起作用。如何从列表 1 或列表 2 中获取随机数?
解决方案
您可以连接列表:
num = random.choice(list_1 + list_2)
或者选择一个列表,然后选择一个字符:
num = random.choice(random.choice([list_1],[list_2]))
推荐阅读
- azure - 我可以使用 Azure Active Directory B2C 通过 SAML 对 SQL 数据库用户表和 ADFS 进行身份验证吗?
- python - 如何在 django 的子类中使用与父类不同的装饰器?
- node.js - 使用 multer 进行图像上传时 req.file 未定义
- c# - 自动解决 Nuget 包版本冲突 C#
- python - 如何在 x 轴上绘制垂直直方图投影?
- c++ - 调用它的函数的 void 指针堆栈
- android - “清单合并失败并出现多个错误,请参阅日志”错误,Arcore 使用 Unity
- powerbi - Group by 的最大功能在 Power BI 中不起作用
- postgresql - 是否有一种从 postgresql 读取数据并在 spark 中对其执行转换的优化方式
- asp.net-mvc - 需要 Azure 广告中的管理员批准