新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何查找缺失的參數(shù)
說明

創(chuàng)新互聯(lián)是一家專業(yè)提供甘谷企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、網(wǎng)站設(shè)計、HTML5建站、小程序制作等業(yè)務(wù)。10年已為甘谷眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
1、如果在切片時遺漏了任何參數(shù),Python將嘗試自動計算。
2、如果檢查CPython的源代碼,會發(fā)現(xiàn)一個函數(shù)叫做PySlice_GetIndicesEx(),它計算任何給定參數(shù)的切片索引。
它是Python中的邏輯等效代碼。
該函數(shù)使用Python對象和可選參數(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ù)的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
當前標題:創(chuàng)新互聯(lián)Python教程:python如何查找缺失的參數(shù)
文章源于:http://m.5511xx.com/article/ccigdoi.html


咨詢
建站咨詢
