我希望我的程序能夠從1到X中的數字,並隨機將這些數字在X/2個列表中的Y個列表之間分配。我不希望在一次洗牌過程中重複一個數字,也不希望這些列表重複。因此,如果列表[1,2]不應該在同一個shuffle中包含1或2的另一個列表,並且整個結果中不應該有另一個[1,2]或[2,1]。在Python查詢中生成隨機列表
這是我想出來的,但是,它不斷重複數字。任何adice?
import random
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
numberslist = range(1,amount+1)
twos = []
thisshuffle = []
final = []
while len(final) < (amount/2)*times:
twos = []
thisshuffle = []
while len(twos) < 2:
if len(numberslist)!=0:
randomnumber = random.choice(numberslist)
if (randomnumber in twos) or (randomnumber in thisshuffle):
numberslist.remove(randomnumber)
else:
twos.append(randomnumber)
thisshuffle.append(randomnumber)
numberslist.remove(randomnumber)
else:
numberslist = range(1,amount+1)
if (twos or list(reversed(twos))) not in final:
final.append(twos)
k=0
for i in range(times): #this shit prints shit
print "%s:" % (i+1), final[k:k+amount/2]
print
k = k + amount/2
Shuffler()
Shuffler()