0
import random
def iterate_thru_list():
   i = 0  
   L1 = [1,2,3,4,5,6,7,8,9]
   L2=['a','b','c','d','e','f','g','h','i']
   L3= ['A','B','C','D','E','F','G','H','I']
   random.shuffle(L1)
   random.shuffle(L2)
   random.shuffle(L3)


   print ("List:")
   while i <= 5:
      for x, y, z in [(x,y,z) for x in L1 for y in L2 for z in L3]:
         print(x,y,z)
   i = i + 1

I want to iterate through separate lists returning a randomly chosen digit or letter from each and return a 'set' in this case of three unique letters or numbers. Caution the while loop does not work - this loops until it has returned all combinations, which I do not understand either. Can I use random.choice(L1 or L2 or L3) to return x,y and z? Is there another simpler way to return a random selection from multiple lists? THank you for your help

4

1 回答 1

4

Something like?

L1 = [1,2,3,4,5,6,7,8,9]
L2 = ['a','b','c','d','e','f','g','h','i']
L3 = ['A','B','C','D','E','F','G','H','I']

from random import choice

for i in range(5):
    print list(map(choice, (L1, L2, L3)))

[4, 'h', 'A']
[7, 'b', 'G']
[3, 'c', 'C']
[6, 'f', 'H']
[5, 'b', 'A']
于 2013-01-30T16:06:46.357 回答