Python-list.remove(x)x不在列表中

我正在尝试在Python 3.3中创建一个简单的程序,该程序采用四个名称的列表,并将它们随机分配给列表中的另一个人.例如,如果名称是John,Aaron,Lydia和Robin:

约翰先走,然后选择一个名字.他不能画自己的东西.如果他这样做,他会把它放回去并再次抽签.说约翰画了罗宾的名字.罗宾的名字将从游泳池中淘汰.接下来是亚伦的抽奖.他画约翰.约翰的名字被淘汰了.等等,直到分配了所有名称.

我创建了一个具有四个名称的列表,并为每个名称分配了一个值1-4.但是,从列表中删除时我遇到了一个问题,说该值不存在.

list.remove(x):x不在列表中.

它看起来像这样:

def drawNames():
    import random
    John=1
    Aaron=2
    Lydia=3
    Robin=4
    validNames=[John, Aaron, Lydia, Robin]
    nameDrawn=random.choice(validNames)
    def draw():
        nameDrawn=random.choice(validNames)
    #John's Draw:
    draw()
    if nameDrawn != 1:
        if nameDrawn == 2:
            print("John drew: Aaron")
            validNames.remove(2)
        elif nameDrawn == 3:
            print("John drew: Lydia")
            validNames.remove(3)
        elif nameDrawn == 4:
            print("John drew: Robin")
            validNames.remove(4)
    #Aaron's Draw:
    draw()
    if nameDrawn !=2:
        if nameDrawn ==1:
            print("Aaron drew: John")
            validNames.remove(1)
        elif nameDrawn ==3:
            print("Aaron drew: Lydia")
            validNames.remove(3)
        elif nameDrawn ==4:
            print("Aaron drew: Robin")
            validNames.remove(4)
    #Lydia's Draw:
    draw()
    if nameDrawn !=3:
        if nameDrawn ==1:
            print("Lydia drew: John")
            validNames.remove(1)
        elif nameDrawn ==2:
            print("Lydia drew: Aaron")
            validNames.remove(2)
        elif nameDrawn ==4:
            print("Lydia drew: Robin")
            validNames.remove(4)
    #Robin's Draw:
    draw()
    if nameDrawn !=4:
        if nameDrawn ==1:
            print("Robin drew: John")
            validNames.remove(1)
        elif nameDrawn ==2:
            print("Robin drew: Aaron")
            validNames.remove(2)
        elif nameDrawn ==3:
            print("Robin drew: Lydia")
            validNames.remove(3)
drawNames()

我也尝试过使用名称而不是数字值,这会产生相同的错误.

我也觉得这是一个低效的方案.如果您有更好的建议,我将非常有义务.

解决方法:

使用下面的代码,可能会获得更好的里程;与上面提供的名称相比,它可以扩展到许多名称.

import copy
import random
validNames=["John", "Aaron", "Lydia", "Robin"]

def drawNames(namelist,currentname):
    '''
    namelist: list of names to draw from
    currentname: name of person doing the current draw
    '''
    draw_namelist = copy.copy(namelist) # make a copy to remove person drawing if needed
    if currentname in draw_namelist: # check if the person drawing is in the list
        draw_namelist.remove(currentname) # remove current name if in list
    try:
        drawn_name = random.choice(draw_namelist)
        namelist.remove(drawn_name)
        newnamelist = namelist
        print "Drew {}".format(drawn_name)
        print "New list: {}".format(newnamelist)
    except:
        print "Nobody for me to draw!"
        drawn_name=None
        newnamelist = namelist
    return drawn_name, newnamelist

然后可以按以下方式工作:

In [39]: newlist=["John", "Aaron", "Lydia", "Robin"]

In [40]: name,newlist = drawNames(newlist,"Lydia")
Drew Robin
New list: ['John', 'Aaron', 'Lydia']

In [41]: name,newlist = drawNames(newlist,"John")
Drew Aaron
New list: ['John', 'Lydia']

In [42]: name,newlist = drawNames(newlist,"Aaron")
Drew John
New list: ['Lydia']

In [43]: name,newlist = drawNames(newlist,"Robin")
Drew Lydia
New list: []
上一篇:解析vue2.0的diff算法 - 转载


下一篇:使用Netbeans PHPUnit在远程服务器上进行PHP单元测试,可能吗?