Reputation: 13
I would like to get the following output:
T1: j2
T2: j4
T3: j3
T1: j7
T2: j
T3: j6
T1: j5
I have tried it with a for loop but I am not able to do it. This is my code:
import random
team = ['t1','t2','t3']
task = ["j","j2","j3","j4","j5","j6","j7"]
print (task)
s_task = random.sample(task,len(task))
print (s_task)
for itm in team:
for itm1 in s_task:
print(itm,itm1)
T1: j2
T2: j4
T3: j3
T1: j7
T2: j
T3: j6
T1: j5
Upvotes: 1
Views: 63
Reputation: 5207
If you just need a way to format your output, you could use:
Formatted string literal aka f-string (for Python 3.6 or later) like
for itm in team:
for itm1 in s_task:
print(f"{itm}: {itm1}")
Read more about f-strings here.
If your Python3 is lower than Python3.6, stick with str.format()
and use
print("{}: {}".format(itm, itm1))
Upvotes: 1
Reputation: 20147
Using concatenation,
print(itm, ": ", itm1)
Using Old String formatting,
print("%s: %s"%(itm, itm1))
Using New string formating,
print("{0}: {1}".format(itm, itm1))
import random
team = ['t1','t2','t3']
task = ["j","j2","j3","j4","j5","j6","j7"]
print (task)
s_task = random.sample(task,len(task))
print (s_task)
for itm in team:
for itm1 in s_task:
print(itm, ": ", itm1)
T1: j2
T2: j4
T3: j3
T1: j7
T2: j
T3: j6
T1: j5
Upvotes: 0
Reputation: 82765
Looks like you need itertools.cycle
and random.choice
Ex:
import random
from itertools import cycle
team = ['t1','t2','t3']
task = ["j","j2","j3","j4","j5","j6","j7"]
team = cycle(team)
for _ in range(len(task)):
print("{}: {}".format(next(team), random.choice(task)))
Output:
t1: j5
t2: j5
t3: j
t1: j7
t2: j6
t3: j7
t1: j3
Upvotes: 2
Reputation: 1440
You can use string.format():
import random
team = ['t1','t2','t3']
task = ["j","j2","j3","j4","j5","j6","j7"]
print (task)
s_task = random.sample(task,len(task))
print (s_task)
for itm in team:
for itm1 in s_task:
print("{}: {}".format(itm,itm1))
This should print in yout required format
Upvotes: 0