【发布时间】:2022-12-09 12:48:39
【问题描述】:
iterator=0;
thisdict = {}
for country in countries.split(","):
try:
thisdict.update({country: "1"})
except Exception as e:
logging.info('Error occurred')
return thisdict
我想在 python 中使用接受重复键值对的数据结构。我了解到 defaultdict 是一个不错的选择。我怎样才能达到同样的效果。
更改 thisdict = defaultdict() 无效。
谢谢你。
【问题讨论】:
-
你看过documentation了吗?
defaultdict的构造函数需要一个参数 -
dict和defaultdict结构都不接受重复键。
标签: python dictionary key-value defaultdict