新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonindex()
python 中的index()函數(shù)有助于返回元組中給定元素的索引。我們還可以通過(guò)元組提供搜索的起點(diǎn)和終點(diǎn)。

10年積累的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有花垣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
**tuple.index(element, start, end)** #where the element may be string, number, list, etc
索引()參數(shù):
index()方法采用三個(gè)參數(shù)。此方法的輸出應(yīng)該是指示元素位置的整數(shù)值。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 元素 | 要搜索的元素 | 需要 |
| 開始 | 從此索引開始搜索 | 可選擇的 |
| 目標(biāo) | 搜索元素直到這個(gè)索引 | 可選擇的 |
索引()返回值
如果該方法找到給定元素的多個(gè)匹配項(xiàng),它將只返回第一個(gè)匹配項(xiàng)的索引。
| 投入 | 返回值 | | 元素 | 元素索引 | | 如果沒有元素 | ValueError exception(值錯(cuò)誤異常) |
Python 中index()方法的示例
例 1:如何找到元組中元素的索引?
# alphabet tuple
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 tuple
alphabet = ('a', 'b', 'c', 'd', 'e', 'f')
# index of 'g' in alphabet
indexpos = alphabet.index('g')
print('The index of g:', indexpos)
輸出:
ValueError: alphabet.index('g'): g not in tuple 當(dāng)前文章:創(chuàng)新互聯(lián)Python教程:Pythonindex()
網(wǎng)站URL:http://fisionsoft.com.cn/article/cdoopsp.html


咨詢
建站咨詢
