新聞中心
說(shuō)明

創(chuàng)新互聯(lián)是一家專業(yè)提供甘谷企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為甘谷眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
1、如果在切片時(shí)遺漏了任何參數(shù),Python將嘗試自動(dòng)計(jì)算。
2、如果檢查CPython的源代碼,會(huì)發(fā)現(xiàn)一個(gè)函數(shù)叫做PySlice_GetIndicesEx(),它計(jì)算任何給定參數(shù)的切片索引。
它是Python中的邏輯等效代碼。
該函數(shù)使用Python對(duì)象和可選參數(shù)進(jìn)行切片,并返回切片的開(kāi)始、停止、步長(zhǎng)和長(zhǎng)度。
實(shí)例
def py_slice_get_indices_ex(obj, start=None, stop=None, step=None):
length = len(obj)
if step is None:
step = 1
if step == 0:
raise Exception("Step cannot be zero.")
if start is None:
start = 0 if step > 0 else length - 1
else:
if start < 0:
start += length
if start < 0:
start = 0 if step > 0 else -1
if start >= length:
start = length if step > 0 else length - 1
if stop is None:
stop = length if step > 0 else -1
else:
if stop < 0:
stop += length
if stop < 0:
stop = 0 if step > 0 else -1
if stop >= length:
stop = length if step > 0 else length - 1
if (step < 0 and stop >= start) or (step > 0 and start >= stop):
slice_length = 0
elif step < 0:
slice_length = (stop - start + 1)/(step) + 1
else:
slice_length = (stop - start - 1)/(step) + 1
return (start, stop, step, slice_length)以上就是python查找缺失參數(shù)的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
本文標(biāo)題:創(chuàng)新互聯(lián)Python教程:python如何查找缺失的參數(shù)
當(dāng)前URL:http://m.fisionsoft.com.cn/article/ccigdoi.html


咨詢
建站咨詢
