这里的目标是生成随机配对,这些配对通过迭代保证是唯一的(两个数字不会多次匹配)。
这是我每半年举办一次的谋杀神秘派对,我列表中的每个数字都将替换为一个角色名称,然后与另一个角色匹配,以随机生成另外两个他们有谋杀动机的角色,一种是他们不会谋杀的(因此需要三遍)。
我不确定在末尾的if语句实际上在做什么,尽管数字似乎还可以。就我的目的而言,这可能已经足够好了,但是我正在尝试学习。谢谢。
import random
characters = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]#character list
print("list of all characters:")
print(characters)
print("")
i=1
while i <= 3: #loop three times
random.shuffle(characters)#randomize list
print("random character combinations", i, ":")
for x in range(0,len(characters),2):
pairing = [characters[x], characters[x+1]]
print(pairing)
i+=1
if pairing == pairing: #ensure uniqueness?
continue
示例输出:
list of all characters:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
random character combinations 1 :
[20, 2]
[15, 6]
[11, 18]
[22, 9]
[14, 8]
[12, 5]
[10, 16]
[7, 4]
[17, 21]
[19, 3]
[13, 1]
random character combinations 2 :
[6, 20]
[15, 17]
[19, 13]
[16, 14]
[21, 2]
[9, 10]
[7, 11]
[8, 4]
[18, 3]
[22, 5]
[12, 1]
random character combinations 3 :
[1, 2]
[13, 5]
[12, 18]
[9, 14]
[22, 3]
[7, 8]
[19, 4]
[10, 21]
[11, 15]
[6, 20]
[17, 16]
答案 0 :(得分:2)
要生成随机的唯一对,您可以执行以下操作:
from itertools import combinations
import random
j =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
k = list(combinations(j, 2))
random.shuffle(k)
k
是231对的列表。
答案 1 :(得分:0)
我认为您的问题有点令人困惑,但听起来像是一个有趣的难题。
要重新考虑您的问题,我假设您是
您想自动生成这些。
我不确定这是否满足您的要求,但是我通过创建一个结构来做到这一点,该结构将从其内部集合中随机选择一个项目,从其内部集合中删除该项目并返回该项目(即,它会破坏性地进行自身变异) )。
import random
class SelectsFrom:
def __init__(self, items):
self.items = items
def select_one(self, exclude=set()):
if not self.items:
raise Exception("No items remaining")
available = self.items - exclude
if not available:
raise Exception("No items after exclusions")
selected = random.sample(available, 1)[0]
# remove the selected item from our list
self.items = self.items - {selected}
return selected
然后,通过使用其中的三个结构来管理三个独立的选择流。
names = ['Laure', 'Greg', 'Lashon', 'Allan', 'Saturnina',
'Ruthie', 'Deedra', 'Shawana', 'Kathrin', 'Luna',
'Sandie', 'Waylon', 'Virgilio', 'Karisa', 'Yvette',
'Lamar', 'Brett', 'Delicia', 'Charles', 'Jeromy',
'Eugenie', 'Lawanna'] # from http://listofrandomnames.com
while True:
character_choices = []
try:
# Choose first murder target for character,
# excluding own character from choices
firstMurder = SelectsFrom(set(range(22)))
# Choose second murder target for character,
# excluding own character AND the first murder choice
secondMurder = SelectsFrom(set(range(22)))
# Choose a friend,
# excluding own character AND the both murder choices
wontMurder = SelectsFrom(set(range(22)))
for i in range(22):
first = firstMurder.select_one({i})
second = secondMurder.select_one({i, first})
friend = wontMurder.select_one({i,first,second})
character_choices.append([i, first, second, friend])
break
except Exception as e:
print("UNABLE TO FIND MATCHES, Starting again\n")
character_choices = [] # Reset the choices we have found
# Print the successful choices
for (i, first, second, friend) in character_choices:
print("Character [{}] will murder [{} or {}] but not [{}]".format(
names[i], names[first], names[second], names[friend]))
它产生这样的输出(添加风味的名称):
Character [Laure] will murder [Shawana or Virgilio] but not [Brett]
Character [Greg] will murder [Karisa or Waylon] but not [Saturnina]
Character [Lashon] will murder [Yvette or Lawanna] but not [Virgilio]
Character [Allan] will murder [Sandie or Eugenie] but not [Laure]
Character [Saturnina] will murder [Luna or Deedra] but not [Greg]
Character [Ruthie] will murder [Jeromy or Lashon] but not [Lamar]
Character [Deedra] will murder [Charles or Greg] but not [Allan]
Character [Shawana] will murder [Laure or Luna] but not [Waylon]
Character [Kathrin] will murder [Lawanna or Saturnina] but not [Luna]
Character [Luna] will murder [Delicia or Brett] but not [Jeromy]
Character [Sandie] will murder [Saturnina or Allan] but not [Kathrin]
Character [Waylon] will murder [Lashon or Kathrin] but not [Eugenie]
Character [Virgilio] will murder [Ruthie or Charles] but not [Sandie]
Character [Karisa] will murder [Kathrin or Delicia] but not [Yvette]
Character [Yvette] will murder [Lamar or Karisa] but not [Shawana]
Character [Lamar] will murder [Waylon or Shawana] but not [Ruthie]
Character [Brett] will murder [Allan or Yvette] but not [Lawanna]
Character [Delicia] will murder [Greg or Lamar] but not [Charles]
Character [Charles] will murder [Brett or Laure] but not [Lashon]
Character [Jeromy] will murder [Virgilio or Ruthie] but not [Delicia]
Character [Eugenie] will murder [Deedra or Jeromy] but not [Karisa]
Character [Lawanna] will murder [Eugenie or Sandie] but not [Deedra]