import random
actions = ["act1","act2","act3","act4",
"act5","act6"]
subjects =[,"sub1","sub2","sub3",
"sub4","sub5","sub6"]
s1 = len(actions)
s2 = len(subjects)
num = 0
while True:
if num < (s1*s2)+1:
num += 1
actions += subjects
print(random.sample(actions+subjects,3))
我想用组合对2个列表进行排序,但是当我打印这个代码时,有些输出有时打印相同的动作或子(例如:[act1,act1,sub5]),但我不想这样做。
我希望每个输出都不同,如果组合结束,它应该停止打印。和我想要的其他东西一样,它为每个输出提供数字。例如 : 第1行:[act1,sub3,act,4] 第2行:2. [act2,sub2,act1] ... .... ....
我该怎么做?
答案 0 :(得分:1)
我不确定followind代码是否是您正在寻找的内容,请查看:
import itertools
actions = ["act1","act2","act3","act4","act5","act6"]
subjects =["sub1","sub2","sub3","sub4","sub5","sub6"]
comb = map(' '.join, itertools.chain(itertools.product(actions, subjects)))
i = 0
for element in comb:
print("{}. {}".format(i, element))
i += 1