Python入門:零碎知識點

來源:互聯網
上載者:User

標籤:知識點   形狀   int   number   矩陣轉置   對象   lang   相同   個數   

  • zip()

用於將可迭代的對象作為參數,將對象中對應的元素打包成一個個元組,然後返回由這些元組組成的列表。

如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 * 號操作符,可以將元組解壓為列表。

  1.  a = [1,2,3]
  2.  b = [4,5,6]
  3.  c = [4,5,6,7,8]
  4.  zipped = zip(a,b) # 打包為元組的列表
  5.  print(zipped)
  6.  # [(1, 4), (2, 5), (3, 6)]
  7.   
  8.  print(zip(a,c)) # 元素個數與最短的列表一致
  9.  # [(1, 4), (2, 5), (3, 6)]
  10.   
  11.  print(zip(*zipped)) # 與 zip 相反,*zipped 可理解為解壓,返回二維矩陣式
  12.  # [(1, 2, 3), (4, 5, 6)]
  • np.squeeze()

從數組的形狀中刪除單維條目,即把shape中為1的維度去掉

np.squeeze(images)
  • np.transpose()

矩陣轉置

  1.  arr = np.arange(16).reshape((2, 2, 4))
  2.   
  3.  ‘‘‘
  4.  [[[ 0, 1, 2, 3],
  5.   [ 4, 5, 6, 7]],
  6.   
  7.   [[ 8, 9, 10, 11],
  8.   [12, 13, 14, 15]]]
  9.  ‘‘‘
  10.   
  11.  arr.transpose((1,0,2))
  12.  ‘‘‘
  13.  [[[ 0, 1, 2, 3],
  14.   [ 8, 9, 10, 11]],
  15.   
  16.   [[ 4, 5, 6, 7],
  17.   [12, 13, 14, 15]]]
  18.  ‘‘‘
  19.   
  20.  arr.transpose((0,2,1))
  21.  ‘‘‘
  22.  [[[ 0, 4],
  23.   [ 1, 5],
  24.   [ 2, 6],
  25.   [ 3, 7]],
  26.   
  27.   [[ 8, 12],
  28.   [ 9, 13],
  29.   [10, 14],
  30.   [11, 15]]]
  31.  ‘‘‘
  32.   
  33.  arr.transpose((1,2,0))
  34.  ‘‘‘
  35.  [[[ 0, 8],
  36.   [ 1, 9],
  37.   [ 2, 10],
  38.   [ 3, 11]],
  39.   
  40.   [[ 4, 12],
  41.   [ 5, 13],
  42.   [ 6, 14],
  43.   [ 7, 15]]]
  44.  ‘‘‘

 

www.hanbotec.com
來源瀚博映像轉載請註明

 

Python入門:零碎知識點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.