遍历列表时出现Python类型错误
我正在自学Python3.2,我正在尝试制作一个程序来匹配一个名称列表。pList是一个多维列表,第0列有一个字符串,第1列有一个整数,第2列有一个布尔值。但是,每当我尝试调用此函数(仅当列表中的行数为偶数时才运行)时,就会出现一个TypeError遍历列表时出现Python类型错误,python,object,int,typeerror,iterable,Python,Object,Int,Typeerror,Iterable,我正在自学Python3.2,我正在尝试制作一个程序来匹配一个名称列表。pList是一个多维列表,第0列有一个字符串,第1列有一个整数,第2列有一个布尔值。但是,每当我尝试调用此函数(仅当列表中的行数为偶数时才运行)时,就会出现一个TypeError Traceback (most recent call last): File "C:\Users\METC\Dropbox\assassins.py", line 150, in <module> main() File "C:\
Traceback (most recent call last):
File "C:\Users\METC\Dropbox\assassins.py", line 150, in <module>
main()
File "C:\Users\METC\Dropbox\assassins.py", line 11, in main
update(ops, pList)
File "C:\Users\METC\Dropbox\assassins.py", line 125, in update
b = match(pList)
File "C:\Users\METC\Dropbox\assassins.py", line 47, in match
q, p = 0
TypeError: 'int' object is not iterable
回溯(最近一次呼叫最后一次):
文件“C:\Users\METC\Dropbox\suggers.py”,第150行,在
main()
文件“C:\Users\METC\Dropbox\suggers.py”,第11行,主目录
更新(ops,pList)
更新中第125行的文件“C:\Users\METC\Dropbox\subjects.py”
b=匹配(pList)
文件“C:\Users\METC\Dropbox\suggers.py”,第47行,匹配
q、 p=0
TypeError:“int”对象不可编辑
任何帮助都将不胜感激,但请记住我是这门语言的初学者,所以要温柔。:)我不介意你太过技术化;我有计算机科学方面的经验
def match(pList):
b = []
z = len(pList)-1
for x in range(z):
b.append([pList[x][0],0])
for x in range(z):
isValid = False
q, p = 0
while isValid == False:
q = random.randint(0, z)
print('q is ' + str(q))
if q > z:
isValid = False
elif q < 0:
isValid = False
elif pList[q][1] == True:
isValid = False
else:
isValid = True
isMatch = False
while isMatch == False:
if pList[q][1] == False:
isValid = False
while isValid == False:
p = random.randint(0,z)
print('p is ' + str(p))
if p > z:
isValid = False
elif p < 0:
isValid = False
elif pList[p][2] == True:
isValid = False
else:
if q == p:
isValid = False
else:
isValid = True
print('match valid')
b[q][1] = pList[p][0]
isMatch = True
print('')
return b
def匹配(pList):
b=[]
z=len(pList)-1
对于范围(z)内的x:
b、 追加([pList[x][0],0])
对于范围(z)内的x:
isValid=False
q、 p=0
当isValid==False时:
q=random.randint(0,z)
打印('q为'+str(q))
如果q>z:
isValid=False
elif q<0:
isValid=False
elif pList[q][1]==真:
isValid=False
其他:
isValid=True
isMatch=False
当isMatch==False时:
如果pList[q][1]==False:
isValid=False
当isValid==False时:
p=random.randint(0,z)
打印('p为'+str(p))
如果p>z:
isValid=False
elif p<0:
isValid=False
elif pList[p][2]==真:
isValid=False
其他:
如果q==p:
isValid=False
其他:
isValid=True
打印('匹配有效')
b[q][1]=pList[p][0]
isMatch=True
打印(“”)
返回b
这是您的构造:
q, p = 0
它试图通过迭代来解压单个int。这不是有效的Python语法。我想这个错误可能会更好
使用:
相反。你把逻辑弄得太复杂了,以至于我不得不通过几次才能把它弄清楚,并告诉你你做错了什么 首先,我们将修复实际报告的错误,正如其他人所指出的。同时,我们将应用一个简单的原则:不要与布尔文本进行比较。你不会说“如果真的下雨了,我需要一把伞”。你说“如果下雨,我需要一把伞”。所以别再多吃东西了<代码>如果isValid比
如果isValid==True
更清晰,因为它突出显示了isValid
的确切含义。我还将删除调试跟踪(print
语句,这些语句显然只用于检查代码是否正确;首先简化代码,然后检查的内容就更少了)
你也不应该对布尔人这样做。最后,我们可以而且应该使用逻辑和
和或
来连接布尔条件
def match(pList):
b = []
z = len(pList)-1
for x in range(z):
b.append([pList[x][0],0])
for x in range(z):
isValid = False
q = p = 0
while not isValid:
q = random.randint(0, z)
isValid = not pList[q][1]
isMatch = False
while not isMatch:
if not pList[q][1]:
isValid = False
while not isValid:
p = random.randint(0,z)
isValid = not pList[p][2] and (q != p)
b[q][1] = pList[p][0]
isMatch = True
return b
我的下一步将是修复列表索引。将索引编入列表通常不是您真正想要的,实际上这里引入了一个bug。很明显,您希望迭代pList的每个“行”;但是range(z)
给出了从0
到z-1
的数字,因此在计算z
时,从len(pList)
中减去1是不正确的。例如,如果pList
有5个元素,您将计算z=4
,并生成范围(z)=[0,1,2,3]
。您将永远无法访问pList[4]
,并且b
将只有4个元素
从根本上说,您使用z
做了两件事。一种是使循环运行的次数与pList中的“行”数量相同,并且(在第一个循环中)对每个“行”执行一些操作。这样做
这一点非常重要:range
不是魔术,它与for循环没有特殊的连接。它只是一个生成数字列表的函数。在Python中,for
循环直接提供元素。所有这些索引的废话都是废话,最好留给能力较差的语言。如果要对列表中的每个元素执行某些操作,那么可以对列表中的每个元素执行某些操作,方法是编写循环遍历列表中每个元素的代码。直接的。不要超过一些单独的索引列表,然后使用它们将索引返回到原始索引中。这会让事情变得复杂
使用z
所做的第二件事是生成一个可能的索引的随机数,这样就可以索引到pList
中以获得一个随机行。换句话说,您只需要选择一个随机行。所以只需随机选择一行。random
模块直接提供此功能:该功能称为random.choice
,它的功能与听起来完全一样
这里有一个小问题:原始代码比较p==q
,即比较两个随机选择的列表索引是否相等。如果我们不再索引,那么我们就没有可比较的索引。要解决这个问题,我们需要理解最初的目的是什么:再次检查新选择的行是否实际上是旧选择的行。同样,我们通过直接检查来简化:我们选择新行而不是新索引,然后查看它是否是旧的
我们还有一个问题,那就是我们需要选择一个合适的人
def match(pList):
b = []
z = len(pList)-1
for x in range(z):
b.append([pList[x][0],0])
for x in range(z):
isValid = False
q = p = 0
while not isValid:
q = random.randint(0, z)
if q > z:
isValid = False
elif q < 0:
isValid = False
elif pList[q][1]:
isValid = False
else:
isValid = True
isMatch = False
while not isMatch:
if not pList[q][1]:
isValid = False
while not isValid:
p = random.randint(0,z)
if p > z:
isValid = False
elif p < 0:
isValid = False
elif pList[p][2]:
isValid = False
else:
if q == p:
isValid = False
else:
isValid = True
b[q][1] = pList[p][0]
isMatch = True
return b
if x == 1:
y == 1
elif x == 2:
y == 2
# ... etc. ad infinitum for every possible integer value of x
def match(pList):
b = []
z = len(pList)-1
for x in range(z):
b.append([pList[x][0],0])
for x in range(z):
isValid = False
q = p = 0
while not isValid:
q = random.randint(0, z)
isValid = not pList[q][1]
isMatch = False
while not isMatch:
if not pList[q][1]:
isValid = False
while not isValid:
p = random.randint(0,z)
isValid = not pList[p][2] and (q != p)
b[q][1] = pList[p][0]
isMatch = True
return b
def match(pList):
b = []
for row in pList:
b.append([row[0], 0])
for row in pList:
isValid = False
while not isValid:
first_row, b_row = random.choice(zip(pList, b))
isValid = not first_row[1]
isMatch = False
while not isMatch:
if not first_row[1]:
isValid = False
while not isValid:
second_row = random.choice(pList)
isValid = not second_row[2] and (first_row is not second_row)
b_row[1] = second_row[0]
isMatch = True
return b
def match(pList):
b = []
for row in pList:
b.append([row[0], 0])
for row in pList:
while True:
first_row, b_row = random.choice(zip(pList, b))
if not first_row[1]:
break
while True:
second_row = random.choice(pList)
if not second_row[2] and (first_row is not second_row):
break
b_row[1] = second_row[0]
return b
def match(pList):
b = [[row[0], 0] for row in pList]
for row in pList:
while True:
first_row, b_row = random.choice(zip(pList, b))
if not first_row[1]:
break
while True:
second_row = random.choice(pList)
if not second_row[2] and (first_row is not second_row):
break
b_row[1] = second_row[0]
return b