print(dict(set(vars(args1).items()) - set(vars(args).items()))) TypeError: unhashable type: 'list'
时间: 2023-10-05 11:04:18 浏览: 43
这个错误通常是由于字典中包含了不可哈希的数据类型,比如列表。在你的代码中,args1和args都包含了列表类型的变量,所以无法通过set函数进行比较。要想解决这个问题,你可以考虑将列表转换为元组,因为元组是可哈希的。比如可以使用tuple函数将列表转换为元组,或者使用列表推导式将字典中的列表替换为元组。例如:
```
# 使用 tuple 函数将列表转换为元组
dict(set(tuple(item) if isinstance(item, list) else item for item in vars(args1).items()) - set(tuple(item) if isinstance(item, list) else item for item in vars(args).items()))
# 使用列表推导式将字典中的列表替换为元组
dict(set([(k, tuple(v) if isinstance(v, list) else v) for k, v in vars(args1).items()]) - set([(k, tuple(v) if isinstance(v, list) else v) for k, v in vars(args).items()]))
```
这样就可以避免出现 TypeError: unhashable type: 'list' 的错误了。
相关问题
args = vars(ap.parse_args())
This line of code calls the `parse_args()` method of an `ArgumentParser` object named `ap`, and then passes the resulting object to the `vars()` function. The `vars()` function returns the `__dict__` attribute of the object as a dictionary. This dictionary contains all of the command-line arguments that were parsed by the `ArgumentParser` object, with the argument names as keys and the argument values as values. The `args` variable is assigned this dictionary.
dconfig = vars(args)
在Python中,vars()是一个内置函数,它接受一个对象作为参数,并返回该对象的__dict__属性。如果对象没有__dict__属性,那么vars()会尝试调用该对象的__slots__属性,返回一个字典,其中包含了该对象的属性和属性值。
在这个代码中,我们将args对象作为vars()函数的参数,将args对象的属性和属性值转换为一个字典,并将其赋值给了变量dconfig。因此,dconfig是一个字典类型的变量,其中包含了args对象的属性和属性值。这个字典可以用于进一步处理和使用解析后的命令行参数和选项。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)