python學習筆記1-numpy/enumerate

來源:互聯網
上載者:User

標籤:rate   numpy   lines   color   ros   print   ber   sha   eth   

1. np.size和np.prod

1 import numpy as np2 x = np.zeros((3, 5,  2), dtype=np.complex128)3 # ndarray.size is the number of elements in the array4 # equivalent to np.prod(a.shape)5 print(x.size)6 print(np.prod(x.shape))

2. enumerate()函數是python的內建函數,在字典上進行枚舉、列舉,對於一個可迭代的(iterable)/可遍曆的對象,enumerate將其組成一個索引序列,利用它可以同時獲得索引和值。註:enumerate多用於for迴圈中得到計數。

使用舉例:

(1)對於列表,既要遍曆索引又要遍曆元素時

1 list1 = ["這", "是", "一個", "測試"]2 # method 13 for i in range(len(list1)):4     print(i, list1[i])5 # method 2 use enumerate6 for index, item in enumerate(list1):7     print(index, item)

(2)enumerate還可以接收第二個參數,用於指定索引起始值

1 list1 = ["這", "是", "一個", "測試"]2 for index, item in enumerate(list1, 1):3     print(index, item)4 ‘‘‘5 1 這6 2 是7 3 一個8 4 測試9 ‘‘‘

(3)enumerate用於統計檔案行數

1 # method 12 count = len(open(filepath, ‘r‘).readlines())3 # method 24 count = -15 for index, line in enumerate(open(filepath, ‘r‘)):6     count += 1

 

python學習筆記1-numpy/enumerate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.