成功解决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'
?
?
?
?
目錄
解決問題
解決思路
解決方法
?
?
?
?
?
解決問題
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')] ? )
哈哈,大功告成!
?
?
?
?
?
總結
以上是生活随笔為你收集整理的成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ML之xgboost:基于xgboost
- 下一篇: ML之xgboostGBM:基于xgbo