关于步长切片的问题 [英] question about slicing with a step length

查看:94
本文介绍了关于步长切片的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于:


数字= [1,2,3,4,5,6,7,8,9,10]


有人可以向我解释为什么


数字[10:0:-2]导致[10,8,6,4,2]?

我认为第一个指数,无论是前进还是后退,是

包括在内。这个列表中没有10的索引,那么什么是
指的是什么?


谢谢。

解决方案

John Salerno写道:

鉴于:

数字= [1,2,3,4,5,6,7, 8,9,10]

有人可以向我解释为什么

数字[10:0:-2]导致[10,8,6,4,2] ?

我认为第一个指数,无论是前进还是后退,都是包容性的。这个列表中没有10的索引,所以它指的是什么?

谢谢。




它的引用在列表结束之后,就像0在列表开始之前指示

一样。单词切片应该是

建议在元素之间(或之前或之后)切割。


-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/


John萨勒诺写道:

鉴于:

数字= [1,2,3,4,5,6,7,8,9,10]

数字[10:0:-2]导致[10,8,6,4,2]?



我总是遇到麻烦。鉴于文档[1]:


"""

从步骤k到i的j切片被定义为序列

索引为x = i + n * k的项目


0 \leq n< \frac {J-I} {K}

Given:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

can someone explain to me why

numbers[10:0:-2] results in [10, 8, 6, 4, 2]?

I thought the first index, whether going forward or backward, was
inclusive. And there is no index of 10 in this list, so what is it
referring to?

Thanks.

解决方案

John Salerno wrote:

Given:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

can someone explain to me why

numbers[10:0:-2] results in [10, 8, 6, 4, 2]?

I thought the first index, whether going forward or backward, was
inclusive. And there is no index of 10 in this list, so what is it
referring to?

Thanks.



Its referring to right after the end of the list, just as the 0 refers
to just before the start of the list. The word "slice" is supposed to
suggest cutting between (or before or after) elements.

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


John Salerno wrote:

Given:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

can someone explain to me why

numbers[10:0:-2] results in [10, 8, 6, 4, 2]?



I always have trouble with these. Given the docs[1]:

"""
The slice of s from i to j with step k is defined as the sequence of
items with index x = i + n*k such that


0 \leq n < \frac{j-i}{k}


这篇关于关于步长切片的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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