【发布时间】:2018-05-08 22:19:52
【问题描述】:
这是一个相当基本的问题,因为我是来自 R 的 Python 新手。
在 R 中,我们可以像这样访问向量或列表中的非连续切片:
> a = 1:10
> a[c(2:4, 7:9)]
[1] 2 3 4 7 8 9
> list_a = list(1:10)
> list_a[[1]][c(2:4, 7:9)]
[1] 2 3 4 7 8 9
我正在尝试找出如何在 Python 中对列表执行相同操作。
例如
a = list(range(20))
a[1:4]
# returns
[1, 2, 3]
# but the following syntax creates an exception:
a[1:4, 7:9]
您的建议将不胜感激。
【问题讨论】:
-
一些建议可以在这里找到:stackoverflow.com/questions/22412509/…,我特别喜欢使用 operator.itemgetter,这似乎是最有效的