简体   繁体   English

在 csp python 中将字典作为参数传递给不可散列的类型:'dict'

[英]passing dictionary as parameter in csp python gives unhashable type: 'dict'

I am applying the CSP in python having problem when passing dictionary as a parameter to a method in python我在 python 中应用 CSP 在将字典作为参数传递给 python 中的方法时出现问题

officerDomain = range(0, 2**scheduleDays-1)

solver = MinConflictsSolver()
problem = Problem(solver)

officers = []
officers.append({'name':'Ryan', 'rank':'3'})
officers.append({'name':'Mond', 'rank':'1'})
officers.append({'name':'Eric', 'rank':'3'})


for officer in officers:
    print(officer)
    problem.addVariable(officer, officerDomain)

While passing the dictionary inside the list it gives the error unhashable type: 'dict'在列表中传递字典时,它会给出错误unhashable type: 'dict'

Let's read the documentation :让我们阅读文档

addVariable(self, variable, domain)

Parameters : variable - Object representing a problem variable (type=hashable object)参数变量- Object 表示问题变量(类型=哈希对象)

So you can't use a dictionary (officer in this case) because it's unhashable.所以你不能使用字典(在这种情况下是官员),因为它是不可散列的。 Try with officer["name"] for example.例如,尝试使用officer["name"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM