python - List comprehension to generate and then sample from array that in turn depends on values in another array (index dependent) -
is there way perform list comprehension each item in list generated sampling randomly list in turn generated depending on values of 2 other lists @ particular index? realize readability suffer, i'm curious if can following list comprehension:
def goat_door(guess, correct): doorlist = [] in range(len(guess)): items = [1,2,3] if(guess[i] in items): items.remove(guess[i]) if(correct[i] in items): items.remove(correct[i]) doorlist.append(random.choice(items)) return doorlist
(the famous 3 door guessing game problem).
the input list guess represents n guesses prize door (independent) , correct actual prize door these n guesses. function goat_door chooses door neither guess nor price door..
i'm new python trying push list comprehension. can done 1 or 2 list comprehensions?
def goat_door(guess, correct): return [random.choice(list({1,2,3} - set(gc))) gc in zip(guess, correct)]
or
def goat_door(guess, correct): return [random.choice(list({1,2,3} - {g,c})) g,c in zip(guess, correct)]
Comments
Post a Comment