Skip to main content
 首页 » 编程设计

python之来自两个列表的随机元素之Python

2024年12月31日8xing901022

所以我在 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]))