2012-08-30 40 views
0

我得到“unhashable”错误,当我做了以下内容:“表%制造者%”如何赋值键

a = {} 
a["wer":"table.%%maker%%"] 

Traceback (most recent call last): 
    File "<pyshell#2>", line 1, in <module> 
    a["wer":"table.%%maker%%"] 
TypeError: unhashable type 

“WER”键应有的价值在这里,但我不能插入百分号。我应该怎么做?

+1

我花了一些时间才弄清楚_actually_发生了什么ERE。 –

+2

相关:[为什么Python会引发TypeError而不是SyntaxError?](http://stackoverflow.com/q/4157278/20862) –

+0

这与百分比符号无关! a [2:3]给出相同的错误。 –

回答

8

可以在字典键使用字符%,但是分配的值是错误的。

>>> my_dict = {} 
>>> my_dict['wer'] = 'table.%maker%' 
>>> my_dict 
{'wer': 'table.%maker%'} 

您可以使用符号用:是这样的:

>>> my_dict = {'wer': 'table.%maker%'} 
>>> my_dict 
{'wer': 'table.%maker%'} 

Python有它描述了如何使用数据结构here一个伟大的文档。

+0

对。谢谢.. – alwbtc

1

使用此值分配给“WER”键:

a = {"wer":"table.%%maker%%"} 

或之后:

a["wer"] = "table.%%maker%%" 
1

您可以在字典的建设(INIT)时设置的值字典已使用下标运算符构建:

a = {} 
a["wer"] = "table.%%maker%%"