Why does
pickle.dumps({}.items())
失败与TypeError: can't pickle dict_items objects
在 Python 3.5.2 中但不在 Python 2.7.12 中?
用“酸洗”字典
pickle.dumps({})
适用于两个 Python 版本(并且在 Python 2.7.12 中给出与上述命令相同的输出)。
因为在Python 2.7中.items()
仅返回一个list
of tuples
, which is可腌制的。
在 python 3.x 中它返回一个dict_items
对象(python 2中不存在),不可picklable(但速度更快,因为它不生成列表,它大致相当于python 2.xiteritems()
).
但您可以强制列表转换来模拟 python 2.x 行为:
pickle.dumps(list(d.items()))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)