首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中,如何获取数组中均匀分布的多个不相邻的行或列?

在Python中,如何获取数组中均匀分布的多个不相邻的行或列?
EN

Stack Overflow用户
提问于 2018-06-15 08:46:59
回答 1查看 62关注 0票数 0

假设我有一个10 * 10的数组x,在Matlab中,我可以使用诸如x(1 : 3 : 7, 2 : 4 :10)这样的代码来实现跨步访问。我如何在Python中做同样的事情?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 08:56:32

使用numpy

代码语言:javascript
复制
import numpy as np

simple_array = np.arange(6)
print(simple_array)

# array[startIndex : endIndex : stepValue]*
print(simple_array[1:5:2])

# if you want to include the last value, you need to go above it
print(simple_array[1:6:2])

# obtain the first n-elements using array[:n]. Note that the n-th element isn't included
print(simple_array[:4])

# or the elements after that with array[n:]. Note that the n-th element is included
print(simple_array[4:])

# use step value with array[::n]
print(simple_array[::2])

# this will return the columns 3-5, with 5 not being included
print(simple_array[3:5])

# here we start at column 1, with a step of 2
print(simple_array[1::2])

这将输出:

代码语言:javascript
复制
[0, 1, 2, 3, 4, 5]
[1, 3]
[1, 3, 5]
[0, 1, 2, 3]
[4, 5]
[0, 2, 4]
[3, 4]
[1, 3, 5]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50867793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档