简体   繁体   中英

Python use variable as dict key with equality syntax

I have:

dict_ = dict(a="dog")

How can I add a variable BATCH_SIZE = "*batch_size" to this dictionary?

If I just do:

dict_ = dict(a="dog",
BATCH_SIZE=32)

If will treat BATCH_SIZE as a string and not a reference. I want it to treat BATCH_SIZE as a variable/reference.

try this:

dict_ = {a: "dog"}
dict_[BATCH_SIZE] = 32

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