Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查元素是否在列表中时出错_Python_List_If Statement_File Io_Io - Fatal编程技术网

Python 检查元素是否在列表中时出错

Python 检查元素是否在列表中时出错,python,list,if-statement,file-io,io,Python,List,If Statement,File Io,Io,我试图检查某些元素是否在列表中,并执行数字更新,但我一直得到一个错误(如下) 由于len(h2ooutput)==2,它只有两个位置,在python中从零开始,因此h2ooutput[2]是不受限制的,索引必须是0或1您在h2ooutput[1]和h2ooutput[2]中有硬编码索引。其中任何一个都是问题的根源。请检查列表的大小 删除if条件下的真布尔值,因为它是不必要的。在为列表编制索引之前,使用len()检查列表的长度什么是h1和h2?您也不需要==True语句,您已经在用h2oout

我试图检查某些元素是否在列表中,并执行数字更新,但我一直得到一个错误(如下)




由于
len(h2ooutput)==2
,它只有两个位置,在python中从零开始,因此
h2ooutput[2]
是不受限制的,索引必须是
0
1
您在
h2ooutput[1]
h2ooutput[2]
中有硬编码索引。其中任何一个都是问题的根源。请检查列表的大小


删除if条件下的真布尔值,因为它是不必要的。

在为列表编制索引之前,使用
len()
检查列表的长度什么是
h1
h2
?您也不需要
==True
语句,您已经在用
h2ooutput[1]检查是否不在h1output
中,这将计算为
True
False
似乎h1output是一个列表,而不是set或dict,或者使用
list.index()
,但是
list.index()
将循环列表。欢迎使用StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。在if条件下删除真正的布尔值。在h2output[1]和h2output[2]中有硬编码索引。其中任何一个都是问题的根源。请检查列表的大小。
"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