1. <source id="nejs2"></source>
  2. <video id="nejs2"></video>
    <source id="nejs2"></source>
          1. Python index()

            更新:

            python 中的index()函數有助于返回列表中給定元素的索引。我們還可以通過列表提供搜索的起點和終點。

             **list.index(element, start, end)** #where the element may be string, number, list, etc 
            

            索引()參數:

            該方法采用三個參數。此方法的輸出應該是指示元素位置的整數值。

            參數 描述 必需/可選
            元素 要搜索的元素 需要
            開始 從此索引開始搜索 可選擇的
            目標 搜索元素直到這個索引 可選擇的

            索引()返回值

            如果該方法找到給定元素的多個匹配項,它將只返回第一個匹配項的索引。

            | 投入 | 返回值 | | 元素 | 元素索引 | | 如果沒有元素 | ValueError exception(值錯誤異常) |

            Python 中index()方法的示例

            示例 1:索引()在 Python 中是如何工作的

             # alphabet list
            alphabet = ['a', 'b', 'c', 'e', 'd', 'e', 'f']
            
            # index of 'c' in alphabet
            indexpos = alphabet.index('c')
            print('The index of c:', indexpos)
            
            # element 'e' is searched
            # index of the first 'e' is returned
            indexpos = alphabet.index('e')
            
            print('The index of e:', indexpos) 
            

            輸出:

             The index of c: 2
            The index of e: 3 

            示例 2:如果搜索到的元素不在列表中,則索引為()

             # alphabet list
            alphabet = ['a', 'b', 'c', 'd', 'e', 'f']
            
            # index of'g' is alphabet
            indexpos = alphabet.index('g')
            print('The index of g:', indexpos) 
            

            輸出:

             ValueError: 'g' is not in list 

            示例 3:通過給出開始和結束參數來處理索引()

             # alphabets list
            alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'd']
            
            # index of 'c' in alphabets
            indexpos = alphabets.index('c')   # 2
            print('The index of c:', indexpos)
            
            # 'd' after the 4th index is searched
            indexpos = alphabets.index('i', 4)   # 7
            print('The index of d:', indexpos)
            
            # 'd' between 4rd and 6th index is searched
            indexpos = alphabets.index('d', 4, 6)   # Error!
            print('The index of d:', indexpos) 
            

            輸出:

             The index of c: 2
            The index of d: 7
            Traceback (most recent call last):
              File "*lt;string>", line 13, in 
            ValueError: 'd' is not in list 
            頂部 久久久久99精品成人片毛片_黃色A片三級三級三級无码_日本不卡高清视频v中文字幕_高清欧美视频一区二区
            1. <source id="nejs2"></source>
            2. <video id="nejs2"></video>
              <source id="nejs2"></source>