Python入門:零碎知識點
阿新 • • 發佈:2018-08-05
矩陣 ber pos 二維 () ans transpose arr code
-
zip()
用於將可叠代的對象作為參數,將對象中對應的元素打包成一個個元組,然後返回由這些元組組成的列表。
如果各個叠代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 * 號操作符,可以將元組解壓為列表。
- a = [1,2,3]
- b = [4,5,6]
- c = [4,5,6,7,8]
- zipped = zip(a,b) # 打包為元組的列表
- print(zipped)
- # [(1, 4), (2, 5), (3, 6)]
- print(zip(a,c)) # 元素個數與最短的列表一致
- # [(1, 4), (2, 5), (3, 6)]
- print(zip(*zipped)) # 與 zip 相反,*zipped 可理解為解壓,返回二維矩陣式
- # [(1, 2, 3), (4, 5, 6)]
-
np.squeeze()
從數組的形狀中刪除單維條目,即把shape中為1的維度去掉
np.squeeze(images)
-
np.transpose()
矩陣轉置
- arr = np.arange(16).reshape((2, 2, 4))
- ‘‘‘
-
[[[ 0, 1, 2, 3],
- [ 4, 5, 6, 7]],
- [[ 8, 9, 10, 11],
- [12, 13, 14, 15]]]
- ‘‘‘
- arr.transpose((1,0,2))
- ‘‘‘
- [[[ 0, 1, 2, 3],
- [ 8, 9, 10, 11]],
- [[ 4, 5, 6, 7],
- [12, 13, 14, 15]]]
- ‘‘‘
- arr.transpose((0,2,1))
- ‘‘‘
-
[[[ 0, 4],
- [ 1, 5],
- [ 2, 6],
- [ 3, 7]],
- [[ 8, 12],
- [ 9, 13],
- [10, 14],
- [11, 15]]]
- ‘‘‘
- arr.transpose((1,2,0))
- ‘‘‘
- [[[ 0, 8],
- [ 1, 9],
- [ 2, 10],
- [ 3, 11]],
- [[ 4, 12],
- [ 5, 13],
- [ 6, 14],
- [ 7, 15]]]
- ‘‘‘
www.hanbotec.com
來源瀚博圖像轉載請註明
Python入門:零碎知識點