简体   繁体   中英

How to assign values to keys

I get "unhashable" error when I do the following:

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" key should have "table.%maker%" value here, but I can't insert percentage signs. Wat should I do?

You can use % characters in dictionary keys, however you assign the value wrong.

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

You can use the notation with : like this:

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

Python has a great documentation which describes how to use data structures here .

使用此命令将值分配给“ wer”键:

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

You can either set the value during the construction (init) of the dictionary:

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

Or after the dict has been constructed, using the subscript operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM