我试图检查某些元素是否在一个列表中,并执行数字更新,但我不断得到一个错误(如下)。你知道吗
"if h2output[1] not in h1output == True or h2output[2] not in h1output == True:
IndexError: list index out of range"
doublewin = 0
h1output = []
h2output = []
h3output = []
v1output = []
v2output = []
v3output = []
d1output = []
d2output = []
for i in h1:
if i not in h1output:
h1output.append(i)
if len(h1output) == 2:
doublewin += 1
for i in h2:
if i not in h2output:
h2output.append(i)
if len(h2output) == 2:
if h2output[1] not in h1output == True or h2output[2] not in h1output == True:
doublewin += 1
在
h2output[1]
和h2output[2]
中有硬编码索引。他们中的任何一个都是问题的根源。请检查一下名单的大小。你知道吗去掉if条件中的真布尔,因为它是不必要的。你知道吗
由于
len(h2output)==2
,它只有两个位置,在python中,这两个位置从零开始,因此h2output[2]
超出界限,索引必须是0
或1
相关问题 更多 >
编程相关推荐