成功解决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
|
7月前
|
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
358 0
|
5月前
|
Python
stack=s+stack#TypeError: can only concatenate str (not “list“) to str
stack=s+stack#TypeError: can only concatenate str (not “list“) to str
|
5月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
146 0
|
7月前
|
JSON 数据格式 Python
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
187 1
|
7月前
|
Python
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
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?
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
336 0
TypeError: cannot concatenate ‘str‘ and ‘list‘ objects
TypeError: cannot concatenate ‘str‘ and ‘list‘ objects