Python 如何解决;TypeError:列表索引必须是整数或切片,而不是tuple";比较两个不同列表中的元组时?

Python 如何解决;TypeError:列表索引必须是整数或切片,而不是tuple";比较两个不同列表中的元组时?,python,list,tuples,typeerror,networkx,Python,List,Tuples,Typeerror,Networkx,我想将一个列表(在无重复列表中)中的所有元组与另一个列表(totallist)进行比较,以查看无重复列表中的元组在totallist中是否为非 这就是没有傻瓜的样子 这就是Totalist的样子 错误看起来如何 如何解决此错误?此错误是由以下事实引起的:x是一个元组,不能将元组用作totallist的索引。这必须是一个整数 如果通过循环两个列表直接比较元组,则可以避免此问题 for x in no_dupes: #looks at tuple in list if x != to

我想将一个列表(在无重复列表中)中的所有元组与另一个列表(totallist)进行比较,以查看无重复列表中的元组在totallist中是否为

这就是没有傻瓜的样子

这就是Totalist的样子

错误看起来如何


如何解决此错误?

此错误是由以下事实引起的:x是一个元组,不能将元组用作totallist的索引。这必须是一个整数

如果通过循环两个列表直接比较元组,则可以避免此问题

for x in no_dupes: #looks at tuple in list 
    if x != totallist[x]: #checks when a tuple in no_dupes is not in totallist 
        return "The graph violates the STC" #ends the function, because as soon as one tuple in no_dupes isn't in totallist, the graph violats the STC.
在本例中,输出为

no_dupes = [(1,2), (3,4), (5,6)]
totallist = [(8,9), (7,8), (6,7), (5,6), (4,5), (3,4), (2,3), (1,2)]

for tup in no_dupes:
    found = False;
    for other_tup in totallist:
        if tup == other_tup:
            found = True
            break
    if found:
        print "Tuple", str(tup), "was found"
    else:
        print "Tuple", str(tup), "was not found"

欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。您不能使用元组为列表中的项目编制索引。元组可以用作字典键。您是否尝试过totallist中的
x
Tuple (1, 2) was found
Tuple (3, 4) was found
Tuple (5, 6) was found