【说站】python如何查找缺失的参数
Python 如何 参数 查找 缺失
2023-06-13 09:13:22 时间
python如何查找缺失的参数
说明
1、如果在切片时遗漏了任何参数,Python将尝试自动计算。
2、如果检查CPython的源代码,会发现一个函数叫做PySlice_GetIndicesEx(),它计算任何给定参数的切片索引。
它是Python中的逻辑等效代码。
该函数使用Python对象和可选参数进行切片,并返回切片的开始、停止、步长和长度。
实例
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查找缺失参数的方法,希望对大家有所帮助。
相关文章
- 在pycharm中如何新建Python文件?_github下载的python源码项目怎么用
- python中dtype什么意思_NumPy Python中的数据类型对象(dtype)
- java与python-如何对比Python和Java,只需三分钟告诉你!
- unboundlocalerror python_Python问题:UnboundLocalError: local variable ‘xxx’ referenced before assignme
- 【说站】python类如何实例化对象
- 【说站】python参数如何传递默认值
- 【说站】python默认参数如何使用
- 【说站】python finally语句如何使用
- 【说站】python循环遍历如何理解
- 【说站】python静态web服务器如何实现
- 【说站】Python argparse库的基本使用步骤
- python学习——pandas查看数据集null值:isnull
- 人生苦短,我用Python-手把手教你如何使用python写串口调试助手
- 这么多年,总算搞清楚了 Python 参数是如何传递的
- python-数据库编程-如何在Python中连接到数据库
- python常见面试题(一)详解编程语言
- Linux下如何快速卸载Python环境(linux如何卸载python)
- Python 调试器入门
- 如何在你的 Python 游戏中添加一个玩家
- Linux环境下Python开发的历程(linux与python)
- Linux中如何离开Python环境(linux怎么退出python)
- Linux下运行Python脚本的步骤简介(linux如何运行python脚本)
- 从零学python系列之教你如何根据图片生成字符画
- Python常用正则表达式符号浅析