Typeerror:list indices必须是整数,而不是元组

时间:2015-10-10 05:50:40

标签: python runtime-error

我的指数类型有错误。 我试着运行它已经编译的代码,但是你可以在下面的Python IDLE中看到错误。

守则:

def triples(self, (sub, pred, obj)):

    #Generator over the triple store.
    #Returns triples that match the given triple pattern. 

    # check which terms are present in order to use the correct index:
    try:
        if sub != None: 
            if pred != None:
                # sub pred obj
                if obj != None:
                    if obj in self._spo[sub][pred]: yield (sub, pred, obj)
                # sub pred None
                else:
                    for retObj in self._spo[sub][pred]: yield (sub, pred, retObj)
            else:
                # sub None obj
                if obj != None:
                    for retPred in self._osp[obj][sub]: yield (sub, retPred, obj)
                # sub None None
                else:
                    for retPred, objSet in self._spo[sub].items():
                        for retObj in objSet:
                            yield (sub, retPred, retObj)
        else:
            if pred != None:
                # None pred obj
                if obj != None:
                    for retSub in self._pos[pred][obj]:
                        yield (retSub, pred, obj)
                # None pred None
                else:
                    for retObj, subSet in self._pos[pred].items():
                        for retSub in subSet:
                            yield (retSub, pred, retObj)
            else:
                # None None obj
                if obj != None:
                    for retSub, predSet in self._osp[obj].items():
                        for retPred in predSet:
                            yield (retSub, retPred, obj)
                # None None None
                else:
                    for retSub, predSet in self._spo.items():
                        for retPred, objSet in predSet.items():
                            for retObj in objSet:
                                yield (retSub, retPred, retObj)
    # KeyErrors occur if a query term wasn't in the index, so we yield nothing:
    except KeyError:
        pass

在Python IDLE中执行后出现以下错误。错误我认为Python中的指数IDLE。

list(movie_graph.triples((" blade_runner"," directed_by",None)))[(" blade_runner"," directed_by&#34 ;," ridle_scott&#34)]

追踪(最近一次通话)      文件"",第1行,in     列表(movie_graph.triples((" blade_runner"" directed_by",无)))[(" blade_runner"" directed_by",& #34; ridle_scott&#34)]  TypeError:list indices必须是整数,而不是元组

1 个答案:

答案 0 :(得分:2)

您获得的错误与triples函数无关。这条线本身就是错误的:

list(movie_graph.triples(("blade_runner","directed_by",None))) [("blade_runner","directed_by","ridle_scott")]

list(...)返回一个列表,以下方括号[...]正在尝试索引该列表。列表索引必须是整数,而不是("blade_runner","directed_by","ridle_scott"),这是一个元组,因此您会看到错误。

很难确切地看到您想要实现的目标,但这就是您看到错误的原因。