Python 中的 slice() 函数有什么作用? [英] What does the slice() function do in Python?

查看:54
本文介绍了Python 中的 slice() 函数有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想澄清一个问题:它是关于 slice() 函数,不是 列表或字符串的切片,如 a[5:4:3].

First of all, I'd like to clarify the question: it's about the slice() function, not slices of lists or strings like a[5:4:3].

文档提到这个函数在 NumPy 中使用,但没有给出使用示例(它说如何使用它,但没有说何时使用它).此外,我从未见过在任何 Python 程序中使用此函数.

The docs mention that this function is used in NumPy and give no examples of usage (it's said how to use it but it's not said when to use it). Moreover, I've never seen this function used in any Python program.

在使用纯 Python(没有 NumPy 或 SciPy)编程时,什么时候应该使用 slice() 函数?任何示例将不胜感激.

When should one use the slice() function when programming in plain Python (without NumPy or SciPy)? Any examples will be appreciated.

推荐答案

a[x:y:z] 给出与 a[slice(x, y, z) 相同的结果].slice 对象的优点之一是它可以作为单个对象存储和检索,而不是存储 xy 和 <代码>z.

a[x:y:z] gives the same result as a[slice(x, y, z)]. One of the advantages of a slice object is that it can be stored and retrieved later as a single object instead of storing x, y and z.

通常用于让用户定义自己的切片,以后可以应用于数据,而无需处理许多不同的情况.

It is often used to let the user define their own slice that can later be applied on data, without the need of dealing with many different cases.

这篇关于Python 中的 slice() 函数有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆