python - 如何提取数组中的多个切片?

标签 python

我需要从数组中的多个位置提取数据。

一个简单的数组是:-

listing = (4, 22, 24, 34, 46, 56)

我熟悉切片。例如:-

listing[0:3]

会给我:-

(4, 22, 24)

但是我无法取出多个切片。例如:-

listing[0:3, 4:5]

给我

TypeError: tuple indices must be integers not tuples

尽管搜索了两本 Python 书籍和互联网,但我无法找出要使用的语法。

最佳答案

您可以切片两次并加入它们。

listing[0:3] + listing[4:5]

关于python - 如何提取数组中的多个切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38895781/

相关文章:

javascript - 通过 Selenium Python 下载整个网页(html、图像、JS)

python - 简洁灵活的结构体定义

python 3,错误处理urllib请求

python - 需要将随机输出转换为字符串

python - openCV错误模块 'cv2.face'没有属性 'createEigenFaceRecognizer'

python - 在虚拟环境中安装python站 pip 包

python - 是否有与 perl 的 module-starter 等效的 python?

python - 使用 Python 为 ETrade API 生成 oauth_signature

python - 使用数据帧作为 .fillna() 的参数时,是否需要相同的形状?

python - 管道子进程时资源不可用