首页 > 解决方案 > 来自两个列表的随机元素 - Python

问题描述

所以我在 Python 中有两个列表:

import random
list_1 = ['1','2','3']
list_2 = ['4','5','6']
num = random.choice(list_1 or list_2)

这似乎不起作用。如何从列表 1 或列表 2 中获取随机数?

标签: pythonlistrandom

解决方案


您可以连接列表:

num = random.choice(list_1 + list_2)

或者选择一个列表,然后选择一个字符:

num = random.choice(random.choice([list_1],[list_2]))

推荐阅读