Python2.7中dict.values()+dict.values(),在Python3.5中解決辦法
阿新 • • 發佈:2019-02-16
首先來看下在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