简体   繁体   English

从 Python 中的 2 个列表中选择 2 个随机项目,每个列表 1 个

[英]Choosing 2 random items from 2 lists, 1 of each, in Python

I have 2 lists, let's say fruits = [Banana, Apple, Orange] names = [Dan, Guy, May].我有 2 个列表,假设水果 = [香蕉、苹果、橙子] 名称 = [丹、盖伊、梅]。

I want to randomly choose 1 item frim each list, possible return will be - Banana, May我想在每个列表中随机选择 1 个项目,可能的回报是 - Banana, May

I've see answers like: random.我看到的答案如下:随机。 Sample(set([1, 2, 3, 4, 5, 6]), 2) But they all refers to picking 2 items from 1 list, or picking 1 item from multiple lists. Sample(set([1, 2, 3, 4, 5, 6]), 2) 但它们都是指从 1 个列表中选择 2 项,或从多个列表中选择 1 项。 thanks.谢谢。

Is this what you need?这是你需要的吗?

return random.choice(fruits), random.choice(names)

If you need to cover more than two lists:如果您需要涵盖两个以上的列表:

lists = [fruits, names, etc]
return tuple(random.choice(l) for l in lists)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM