成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'

简介: 成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'

解决问题


TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'




解决思路


类型错误:+:'dict_items'和'list'不支持的操作数类型


     有些人会使用这种方法:z = dict(x.items() + y.items())

     这其实就是在内存中创建两个列表,再创建第三个列表,拷贝完成后,创建新的dict,删除掉前三个列表。这个方法耗费性能,而且对于python3,这个无法成功执行,因为items()返回是个对象。




解决方法


Z_dict = a.items() + [('b1', 'b2')]

改为

Z_dict = dict(  list(a.items()) + [('b1', 'b2')]   )


哈哈,大功告成!



相关文章
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
3月前
|
JavaScript
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
71 0
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
181 0
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
|
Linux 开发工具 开发者
有关Linux系统出现Malformed line 1 in source list /etc/apt/sources.list (type)问题的解决方式
以下内容仅代表个人意见,本人亲测有效,如果没有效果可以现将之前的sources.list备份的文件恢复 重要的事情说三遍:记得备份,记得备份,记得备份 本文中所阐述的问题大致是因为由于不正常的关闭影响的sources.list文件的异常,以导致这种问题生成的原因,因此在这里考虑进行重新创建sources.list文件来解决问题的处理方式 本蒟蒻在昨晚做Linux(我用的是Debian,教学要求 )的时候,因为要在 /etc/apt/sources.list里添加一些东西,但是更改完之后,发生了错误:
566 0
有关Linux系统出现Malformed line 1 in source list /etc/apt/sources.list (type)问题的解决方式
SAP QM 执行事务代码QP01,系统报错 -Material type FOOD is not defined for task list type Q-
SAP QM 执行事务代码QP01,系统报错 -Material type FOOD is not defined for task list type Q-
SAP QM 执行事务代码QP01,系统报错 -Material type FOOD is not defined for task list type Q-
|
Java
Cannot instantiate the type List<Integer>
在使用java.util.List; 的时候,把语句写成了: List arr = new List(); 导致错误: Cannot instantiate the type List 正确写法是: List arr = new ArrayList();
1358 0
|
1月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)