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

来源:互联网 发布:mac邮箱无法验证账户 编辑:程序博客网 时间:2024/06/06 08:49

首先来看下在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

原创粉丝点击