我寻找一种将变量保存到文件中的方法(使它们持久用于其他计算)。
我找到了一些解决方案,例如:https ://stackoverflow.com/a/899199/1846113 但是当我在如下列表中实现它时:
import pickle
list = [['cccc',['asd','sdad','sdadas']],['cscc',['asd','sdad','sdadas']]]
pickle.dump(list, outfile)
它给了我这个错误
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1370, in dump
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 600, in save_list
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 615, in _batch_appends
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 600, in save_list
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 615, in _batch_appends
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 739, in save_global
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 811, in whichmodule
TypeError: unhashable type: 'list'
有谁知道问题是什么?还是其他解决方案?
编辑:有解决方案
问题是我在创建列表时出错。我会发布它(这样你就可以笑了)并避免这个愚蠢的错误:我通过使用(丑陋的)函数处理列表的一些元素来创建列表:
def process_element(doc):
processed_value = do_something(doc.pop())
return [doc.pop, processed_value]
正如你们中的一些人已经注意到的那样,我在返回输出时出错:
[doc.pop, processed_value]
我在列表中添加了一个不可散列的方法,给了我错误。正确的版本是:
def process_element(doc):
processed_value = do_something(doc.pop())
return [doc.pop(), processed_value]
谢谢。