您的位置:首页 > 编程语言 > Python开发

Python2.7中dict.values()+dict.values(),在Python3.5中解决办法

2017-12-13 10:48 309 查看
首先来看下在Python2.7中代码:

w={
'a':1,
'b':2,
'c':3
}
b={
'aa':4,
'bb':5,
'cc':6
}
r=w.values()+b.values()
print(r)


输出结果为是一个list:

/usr/bin/python2.7 /home/tream/Desktop/2/tt/test.py
[1, 3, 2, 4, 6, 5]
Process finished with exit code 0
下面是在Python3.5中:
w={ 'a':1, 'b':2, 'c':3 } b={ 'aa':4, 'bb':5, 'cc':6 } r=w.values()+b.values() print(r)
结果:


/usr/bin/python3.5 /home/tream/C3D/C3D-tensorflow/test.py

Traceback (most recent call last):

File “/home/tream/C3D/C3D-tensorflow/test.py”, line 16, in

r=w.values()+b.values()

TypeError: unsupported operand type(s) for +: ‘dict_values’ and ‘dict_values’

解决办法就是先强制转换到解决办法就是转换成list再加,Kidding me?确实是这样的。再见!


w={

‘a’:1,

‘b’:2,

‘c’:3

}

b={

‘aa’:4,

‘bb’:5,

‘cc’:6

}

c=list(w.values())

x=list(b.values())

q=c+x

print(q)

输出结果:


/usr/bin/python2.7 /home/tream/Desktop/2/tt/test.py

[1, 3, 2, 4, 6, 5]

Process finished with exit code 0


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python
相关文章推荐