简体   繁体   中英

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].

I want to randomly choose 1 item frim each list, possible return will be - 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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