def f1():
a = ['one', 'two', 'three', 'four', 'five']
def f2():
b = []
for i in range(5):
if random.choice(a) not in b:
b.append(random.choice(a))
else:
return f2()
print(b)
f2()
f1()
I know that random.choice(a) will change every time it is ran but I need to check a random.choice(a) and append that same random.choice(a) to a list if it is not already present.
CodePudding user response:
You can use random sample to create a new shuffled list:
def f1():
a = ['one', 'two', 'three', 'four', 'five']
def f2():
b = random.sample(a, len(a))
print(b)
f2()
f1()
CodePudding user response:
import random
def f(l):
l1=[]
for i in range(1,6):
if random.choice(l) not in l1:
l1.append(random.choice(l)
else:
return f(l)
l=["one","two","three","four","five"]
f(l)