使用shelf时获取“TypeError:unhashable type:'list'”

时间:2014-04-13 22:25:08

标签: python python-2.7 shelve praw

我正在尝试编写一个代码片段,将ID添加到集合中,这样我就可以看到它是否已被使用,并将该集合存储在一个文件中。我一直试图通过使用Shelve模块来做到这一点,但我遇到了一些麻烦。到目前为止,我有这个代码

import praw
import datetime
import shelve

user_agent ='Removed'
r = praw.Reddit(user_agent=user_agent)
submission = r.get_submission(submission_id='11v36o')
r.login('Removed','Removed')
files = shelve.open("PrawTest3.dat", writeback=True)
print "Opened!"
already_done = {} 
files["already_done"] = ["a","b"]
files.close()
done = set()
print "Running"

while True:
    subreddit = r.get_subreddit('mobilebot')
    all_comments = subreddit.get_comments()
    files = shelve.open("PrawTest2.dat", writeback=True)
    already_done = files["already_done"]
    files.close()
    for comment in all_comments:
        if (comment.body == "Hello") and (comment.id not in already_done) and (comment.id not in done):

            files = shelve.open("PrawTest2.dat", writeback=True)
            comment.reply(' world!')
            already_done = files["already_done"]
            already_done.append(comment.id)

            files[already_done] = already_done
            print "Shelves working"
            a = datetime.datetime.now()
            b = "%s:%s:%s" % (a.hour,a.minute, a.second)
            print "["+b+"]"+"Comment sent!"
            files.sync()
            files.close()

2 个答案:

答案 0 :(得分:1)

Python的列表不能用作shelve或dic的键,因为它没有哈希值。但我认为你的问题只是这一行的一个错字:

files[already_done] = already_done

我想你想要它

files["already_done"] = already_done

答案 1 :(得分:0)

错误在于:

files[already_done] = already_done

python中的

list是可变的。可变类型不能用作字典中的键。在执行already_done之前,将列表files[already_done] = already_done转换为元组,它将正常工作。

这就是我的意思:

>>> a_dict = {}
>>> a_list = [1, 2, 3]
>>> a_dict[a_list] = "Hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'


>>> a_tuple = tuple(a_list)
>>> a_dict[a_tuple] = "Hello"
>>> a_dict
{(1, 2, 3): 'Hello'}

但是,如果您只想让密钥成为字符串“already_done”而不是列表already_done,那么您应该这样做:

files['already_done'] = already_done