Python 无法理解的错误,有三个打印跟踪

Python 无法理解的错误,有三个打印跟踪,python,dictionary,Python,Dictionary,我有一本字典,定义如下: SN = {} SN[0] = {'verb' : 1 } print graph print state print graph[state] 在我的函数中,我制作了3个prints,如下所示: SN = {} SN[0] = {'verb' : 1 } print graph print state print graph[state] (我对这些变量不做任何其他操作)它返回: SN 0 S 这怎么可能?为什么它不回来 SN 0 {'verb' :

我有一本字典,定义如下:

SN = {}
SN[0] = {'verb' : 1 }
print graph
print state
print graph[state]
在我的函数中,我制作了3个
print
s,如下所示:

SN = {}
SN[0] = {'verb' : 1 }
print graph
print state
print graph[state]
(我对这些变量不做任何其他操作)它返回:

SN  
0  
S
这怎么可能?为什么它不回来

SN
0
{'verb' : 1}  
整个代码:

abstraction= {}
abstraction['de'] = ['déterminant']
abstraction['le'] = ['déterminant']
abstraction['un'] = ['déterminant']
abstraction['beau'] = ['adjectif', 'substantif']
abstraction['dodu'] = ['adjectif', 'substantif']
abstraction['grand'] = ['adjectif', 'substantif']
abstraction['méchant'] = ['adjectif', 'substantif']
abstraction['noirs'] = ['adjectif', 'substantif']
abstraction['petit'] = ['adjectif', 'substantif']
abstraction['desseins'] = ['substantif']
abstraction['loup'] = ['substantif']
abstraction['poulet'] = ['substantif']
abstraction['gosse'] = ["n'importe quoi"]
abstraction['mange'] = ['verbe']
abstraction['dort'] = ['verbe']


SN = {}
SN[0] = {'déterminant' : 1 }
SN[1] = {'adjectif' : 1, 'substantif' : 2 }
SN[2] = {'adjectif' : 3, '' : 'ok' }
SN[3] = {'' : 'ok' }

SV = {}
SV[0] = {'verbe' : 1}
SV[1] = {'transitions' : 'SN', '' : 'ok'}
SV[2] = {'' : 'ok'}


def transitions(data, graph, state = 0, transit = []) :
    print 'data avt if data :'
    print data
    if data : 
        if 'transitions' in graph[state] :
            return transitions(data, graph[state]['transitions'], 0, transit)
        symbol = data[0]
        if symbol not in abstraction.keys() : 
            return ['impossible, un des mots n\'est pas reconnu par l\'automate'] 
        for a in abstraction[symbol] : # loop on abstractions
            print graph
            print state
            print graph[state]
            if a in graph[state] :
                state = graph[state][a]
                return transitions(data[1:], graph, state, transit + [a])
        else :  
            return transit + [symbol] + ['impossible'] 
    else : 
        if '' in graph[state] :
            return transit + [graph[state]['']]
        else : return transit + ['impossible']

我认为你这里的问题是
graph==“SN”
,而不是(正如你显然预期的那样)
graph==SN

换句话说,
graph
引用的是一个
str
对象,其值
“SN”
不是
dict
对象,该对象也被名称
SN
引用

因此
图形[0]
是字符串
“SN”
中的第一个字符,即字母
“S”

graph==SN
的情况下

print graph
print state
print graph[state]
将是:

{0: {'verb': 1}} # note: not 'SN'
0
{'verb': 1}

编辑

现在您已经发布了代码,此部分:

SN = {}
SN[0] = {'déterminant' : 1 }
SN[1] = {'adjectif' : 1, 'substantif' : 2 }
SN[2] = {'adjectif' : 3, '' : 'ok' }
SN[3] = {'' : 'ok' }
创建字典

SN = {0: {'déterminant': 1}, 1: {'adjectif': 1, 'substantif': 2},
      2: {'adjectif': 3, '': 'ok'}, 3: {'': 'ok'}}
但是,在下一部分:

SV[1] = {'transitions' : 'SN', '' : 'ok'}
您将字符串
'SN'
分配给键
'transitions'
,而不是实际的字典
SN
。这应该是:

SV[1] = {'transitions': SN, '': 'ok'}
                      # ^ no quote marks

此外,由于所有的键都是从零开始的整数,所以可以考虑使用列表而不是字典,例如:

SN = []
SN.append({'déterminant': 1})
SN.append({'adjectif': 1, 'substantif': 2})
...

这是你的全部密码吗?
graph
state
在哪里定义?这显然不是我的全部代码。Graph和state是我函数的参数:transitions(数据,Graph,state=0,transit=[])(感谢您的回复,很抱歉我没有理解如何使用应答器代码)请发布您的全部代码。好的,但是你能回复我的帖子吗?因为我只有不到10个正面评价,所以我可以回答我自己的问题,而且我的代码太大,无法在评论编辑中发布。sry我可以编辑我的帖子。看起来你是对的。OP写了
SV[1]={'transitions':'SN','''ok'}
,而他可能应该写
SV[1]={'transitions':SN',:'ok'}
。好的,谢谢,我添加了一个eval(),它可以工作。谢谢;)但你的方式可能更好凯文,我去看看。谢谢你的回答。顺便说一句,我不明白为什么我的主题被锁定。。。MODO在这个网站上非常严重@用户3581976,因为您没有包含创建
图形
状态
的代码,因此“缺少足够的信息”。是的,但您仍然可以帮助我!您可以在不使用其余代码的情况下找到解决方案。。。不管怎样,这没关系;)祝您今天过得愉快