Python 3.x 使用python的秘密圣诞老人
我正试图写一个密码,为我的堂兄弟挑选秘密圣诞老人。到目前为止,我的代码只适用于偶数人,但我有奇数人或表兄妹Python 3.x 使用python的秘密圣诞老人,python-3.x,Python 3.x,我正试图写一个密码,为我的堂兄弟挑选秘密圣诞老人。到目前为止,我的代码只适用于偶数人,但我有奇数人或表兄妹 import random def Santa(names, draw): matches= [] while names: giver = names.pop() #chooses giver and removes name receiver = random.choice(draw) #picks random to receive if give
import random
def Santa(names, draw):
matches= []
while names:
giver = names.pop() #chooses giver and removes name
receiver = random.choice(draw) #picks random to receive
if giver != receiver:
matches.append([giver, receiver]) #adds pair
draw.remove(receiver) #removes person from receiving list
else:
names.append(giver) #if names are the same, adds name back to list
return matches
family1=['cousin1', 'cousin2', 'cousin3', 'cousin4']
family2=['cousin5', 'cousin6', 'cousin7','cousin8','cousin9','cousin10']
family3=['cousin11', 'cousin12', 'cousin13','cousin14']
family4=['cousin15', 'cousin16', 'cousin17']
family5=['cousin18', 'cousin19']
names = [ 'cousin1', 'cousin2', 'cousin3', 'cousin4','cousin5']
print(Santa(names,names))
错误为Indexer错误:无法从空序列中进行选择
从这一行:
receiver = random.choice(draw)
我将在未来增加的限制之一是,你不能有你的兄弟姐妹或姻亲。任何关于如何使这项工作适用于单数姓名列表或家庭约束的建议都将非常有用
编辑:
如果有人想做同样的事情,这里是我最后的代码
import random
from copy import copy
def Santa(names, draw):
matches= []
while names:
giver = names.pop()
receiver = random.choice(draw)
if giver[0] != receiver[0]:
if giver[1] != receiver[1]:
matches.append([giver, receiver])
draw.remove(receiver)
else:
names.append(giver)
return matches
family1=['cousin1', 'cousin2', 'cousin3', 'cousin4']
family2=['cousin5', 'cousin6', 'cousin7','cousin8','cousin9','cousin10']
family3=['cousin11', 'cousin12', 'cousin13','cousin14']
family4=['cousin15', 'cousin16', 'cousin17']
family5=['cousin18', 'cousin19']
names = [ ('cousin1', 'parent1'), ('cousin2', 'parent1'), ('cousin3', 'parent1'),('cousin4','parent1'),('cousin5','parent2'),('cousin6','parent2'),('cousin7','parent2'),
('cousin8','parent3'),('cousin9','parent3'),('cousin10','parent3'),('cousin11','parent4'),('cousin12','parent4'),('cousin13','parent4'),('cousin14','parent5'),
('cousin15','parent5'),('cousin16','parent2'),('cousin17','parent2'),('cousin18','parent3'),('cousin19','parent3'),('cousin20','parent2')]
print(Santa(names,copy(names)))
您的问题是,您将相同的
名称
数组传递给了这两个参数。这似乎是有效的,但您必须记住,Python通过引用传递参数,因此这不是两个完全相同的对象,而是同一个对象。当您从赠品中弹出一个项目时,它也会从绘图中消失。请尝试以下操作:
import random
from copy import copy
def Santa(names, draw):
matches= []
while names:
giver = names.pop() #chooses giver and removes name
receiver = random.choice(draw) #picks random to receive
if giver != receiver:
matches.append([giver, receiver]) #adds pair
draw.remove(receiver) #removes person from receiving list
else:
names.append(giver) #if names are the same, adds name back to list
return matches
family1=['cousin1', 'cousin2', 'cousin3', 'cousin4']
family2=['cousin5', 'cousin6', 'cousin7','cousin8','cousin9','cousin10']
family3=['cousin11', 'cousin12', 'cousin13','cousin14']
family4=['cousin15', 'cousin16', 'cousin17']
family5=['cousin18', 'cousin19']
names = [ 'cousin1', 'cousin2', 'cousin3', 'cousin4', 'cousin5']
print(Santa(names,copy(names)))
当然,当您继续为两个参数使用不同的列表时,实际上不需要这样做