将字符串从一个列表移动到另一个列表

时间:2015-06-24 03:17:15

标签: python list

每当我运行程序时,它都会出现错误

Traceback (most recent call last):
  File "C:\Users\mrosales\Downloads\Rock Paper Sissor Tornament.py", line 46, in <module>
    Temp = ClassList[Random2]
IndexError: list index out of range

Traceback (most recent call last):
  File "C:\Users\mrosales\Downloads\Rock Paper Sissor Tornament.py", line 60, in <module>
    Temp = ClassList[Random2]
IndexError: list index out of range

我的代码是关于将字符串从一个列表移动到另一个列表以形成tornamnet的设置游戏

import random
import time
Temp = (" ")
ClassList = ['Noah','Simone','Ji Ho','Thanh','Nathanial','Soo','Mickel','Tuan']
Match1 = [], Match2 = [], Match3 = [] ,Match4 = []
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match1.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match1.append(Temp)

print(Match1)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match2.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match2.append(Temp)

print(Match2)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match3.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match3.append(Temp)

print(Match3)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

time.sleep(1)
Temp = ClassList[Random1]
Match4.append(Temp)
del ClassList[Random1]
Temp = ClassList[Random2]
del ClassList[Random2]
Match4.append(Temp)

print(Match4)
Random1 = random.randrange(0,len(ClassList))
Random2 = random.randrange(0,len(ClassList))
while Random1 == Random2:
    Random1 = random.randrange(0,len(ClassList))

print ("The current match ups are...")
print (Temp)
time.sleep(1)
print (Match1, Match2, Match3, Match4)

任何人都可以发现我可能发出的错误,如果愿意,可以更正吗?

3 个答案:

答案 0 :(得分:4)

从我收集的代码试图做起,我相信下面的代码将实现它。它相当短,但基本上可以做你没有崩溃的事情。

import random

ClassList = ['Noah', 'Simone', 'Ji Ho', 'Thanh', 'Nathanial', 'Soo', 'Mickel', 'Tuan']

# Randomise list order
random.shuffle(ClassList)

# Remove last 2 elements from list and add to new match lists
Match1 = [ClassList.pop(), ClassList.pop()]
Match2 = [ClassList.pop(), ClassList.pop()]
Match3 = [ClassList.pop(), ClassList.pop()]
Match4 = [ClassList.pop(), ClassList.pop()]

print(Match1, Match2, Match3, Match4)

注意,如果你多次运行,你会发现它确实提供了不同的“匹配装置”。

答案 1 :(得分:0)

在迭代它时,您正在修改类列表。当它抛出错误时,你的类中没有人留下来选择len(ClassList)0。

如果我理解你要做什么,你可以创建另一个列表并在迭代原始列表时追加名称,以跟踪谁匹配。或者甚至更好地使用random.shuffle

import random
import time
classlist = ['Noah','Simone','Ji Ho','Thanh','Nathanial','Soo','Mickel','Tuan']
from random import shuffle

def get_random( classlist ):
  shuffle( classlist )
  while classlist:
    yield classlist.pop()

matches = []
match = []

for player in get_random(classlist):
    if len(match) <= 1:
        #print "Adding %s" %player
        match.append(player)
        if len(match) == 2:
            matches.append(match)
            #print "Match %s is full" %len(matches)
            match = []

for x in range(0,len(matches)):
    print "Match %s: %s" %(x,matches[x])

答案 2 :(得分:-1)

解决方案1:
第二次del ClassList[Random1]任务后Temp

解决方案2:
Random2 = random.randrange(0,len(ClassList)-1)会解决您的问题,然后您不需要Random1 != Random2

还有什么:
你应该清理你的代码......

temp = classList.pop(random1)

相当于

temp = classList[random1]
del classList[random1]

使用函数来处理重复的代码:

def get_random():
    random_index = random.randrange(0,len(classList))
    return classList.pop(random_index)

match1 = [get_ramdom(), get_random()]

请勿根据PEP8

使用大写变量名称