新聞中心
我們?cè)谑褂帽砀竦臅r(shí)候,通常會(huì)創(chuàng)建索引,索引可以大大加快數(shù)據(jù)的檢索速度,加快表與表之間的連接。在使用分組和排序時(shí),子句進(jìn)行數(shù)據(jù)檢索可以明顯減少查詢中分組和排序的時(shí)間。之前小編向大家介紹過(guò)在列表起索引作用的index() 函數(shù)(https://www.py.cn/jishu/jichu/21636.html)。其實(shí)在python中,起索引作用的不止index函數(shù),還有find函數(shù),這兩個(gè)函數(shù)有什么區(qū)別呢,下面,小編來(lái)向大家介紹一下。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),南州晴隆企業(yè)網(wǎng)站建設(shè),南州晴隆品牌網(wǎng)站建設(shè),網(wǎng)站定制,南州晴隆網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,南州晴隆網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
一、index()
index()方法語(yǔ)法:
str.index(str, beg=0, end=len(string))
python index()方法檢測(cè)字符串中是否包含字符串str,如果指定beg(開(kāi)始)和end(結(jié)束)范圍,則檢查是否包含在指定的范圍內(nèi)。如果包含字符串則返回開(kāi)始的索引值,否則拋出異常。
使用代碼:
PyDoc_STRVAR(index__doc__,
"S.index(sub [,start [,end]]) -> int\n\
\n\
Like S.find() but raise ValueError when the substring is not found.");
static PyObject *
string_index(PyStringObject *self, PyObject *args)
{
Py_ssize_t result = string_find_internal(self, args, +1);
if (result == -2)
return NULL;
if (result == -1) {
PyErr_SetString(PyExc_ValueError,
"substring not found");
return NULL;
}
return PyInt_FromSsize_t(result);
}二、find()
find()方法語(yǔ)法:
str.find(str, beg=0, end=len(string))
python find()方法檢測(cè)字符串中是否包含字符串str,如果指定beg(開(kāi)始)和end(結(jié)束)范圍,則檢查是否包含在指定范圍內(nèi),如果包含字符串則返回開(kāi)始的索引值,否則返回-1
使用代碼:
PyDoc_STRVAR(rfind__doc__,
"S.rfind(sub [,start [,end]]) -> int\n\
\n\
Return the highest index in S where substring sub is found,\n\
such that sub is contained within S[start:end]. Optional\n\
arguments start and end are interpreted as in slice notation.\n\
\n\
Return -1 on failure.");
static PyObject *
string_rfind(PyStringObject *self, PyObject *args)
{
Py_ssize_t result = string_find_internal(self, args, -1);
if (result == -2)
return NULL;
return PyInt_FromSsize_t(result);
}通過(guò)小編這么一對(duì)比,你知道index函數(shù)和find函數(shù)有什么區(qū)別了嗎?實(shí)際上這倆者內(nèi)部并沒(méi)有什么區(qū)別,只不過(guò)是在沒(méi)有找到對(duì)應(yīng)字符串,是一個(gè)異常,還是返回-1。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python中index和find有什么區(qū)別
當(dāng)前路徑:http://m.fisionsoft.com.cn/article/ccosoes.html


咨詢
建站咨詢
