Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获得;类型错误:不可损坏的类型:';列表'&引用;和shelve一起工作时_Python_Python 2.7_Shelve_Praw - Fatal编程技术网

Python 获得;类型错误:不可损坏的类型:';列表'&引用;和shelve一起工作时

Python 获得;类型错误:不可损坏的类型:';列表'&引用;和shelve一起工作时,python,python-2.7,shelve,praw,Python,Python 2.7,Shelve,Praw,我正在尝试编写一段代码,将ID添加到集合中,以便查看它是否已被使用,并将集合存储在文件中。我一直试图通过使用搁置模块来实现这一点,但我遇到了一些麻烦。到目前为止,我有这个代码 import praw import datetime import shelve user_agent ='Removed' r = praw.Reddit(user_agent=user_agent) submission = r.get_submission(submission_id='11v36o') r.lo

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

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()

错误在以下行中:

文件[已完成]=已完成

python中的
list
是可变的。可变类型不能用作字典中的键。在执行
文件[ready_done]=ready_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'}
>a_dict={}
>>>a_list=[1,2,3]
>>>a_dict[a_list]=“你好”
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:不可损坏的类型:“列表”
>>>元组=元组(列表)
>>>a dict[a tuple]=“你好”
>>>格言
{(1,2,3):'Hello'}
但是,如果您只是希望键是字符串“已完成”,而不是列表
已完成
,则应该执行以下操作:


文件['ready_done']=ready_done

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

files[already_done] = already_done
我想你希望它是这样的

files["already_done"] = already_done

列表不可能是字典的关键,我认为问题应该在第行
文件[已经完成]=已经完成了我不敢相信我没有看到,非常感谢!