负步幅列表切片 [英] negative stride list slices
问题描述
任何人都可以用
负面步幅解释列表切片行为背后的逻辑吗?例如:
Can anyone explain the logic behind the behavior of list slicing with
negative strides? For example:
print range(10)[: - 3:-1]
print range(10)[:-3:-1]
[9,8]
我发现这个结果非常令人惊讶,只想看看
规则写在某处。
谢谢,
-
Dave Abrahams
促进咨询
http://www.boost-consulting.com
推荐答案
David Abrahams写道:
David Abrahams wrote:
任何人都可以解释列表切片行为背后的逻辑
负面步幅?例如:
Can anyone explain the logic behind the behavior of list slicing with
negative strides? For example:
>>>打印范围(10)[: - 3:-1]
>>> print range(10)[:-3:-1]
[9,8]
我发现这个结果非常令人惊讶,并且只想看看
在某处写下的规则。
谢谢,
-
Dave Abrahams
提升咨询
http://www.boost-consulting.com
http://www.python.org/doc/current/ tu ... 00000000000000
" Reid Nichol" < RN ********* @ yahoo.com>在消息中写道
news:r7 ****************** @ news1.mts.net ...
"Reid Nichol" <rn*********@yahoo.com> wrote in message
news:r7******************@news1.mts.net...
David Abrahams写道:
David Abrahams wrote:
任何人都可以解释列表切片行为背后的逻辑与否定步幅吗?例如:
Can anyone explain the logic behind the behavior of list slicing with
negative strides? For example:
>>>打印范围(10)[: - 3:-1]
>>> print range(10)[:-3:-1]
[9,8]
我发现这个结果非常令人惊讶,只想看看
[9,8]
I found this result very surprising, and would just like to see the
rules written down somewhere.
http://www.python.org/doc/current/tu...00000000000000
你可能想参考下一节(该书签与
字符串相关,下一个是列表),但在任何一种情况下,
中没有任何示例教程尚未更新以处理切片包括一个
" stride"作为第三个参数。
-
我实际上并没有阅读我的hotmail帐户,但你可以用
替换hotmail激活如果你真的想联系我。
http://www.python.org/doc/current/tu...00000000000000
You probably meant to reference the next section (that bookmark relates to
strings, the next one is lists), but in either case none of the examples in
the tutorial has yet been updated to deal with slices that include a
"stride" as a third parameter.
--
I don''t actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
David Abrahams写道:
David Abrahams wrote:
任何人都可以解释列表切片行为背后的逻辑
消极的步伐?例如:
Can anyone explain the logic behind the behavior of list slicing with
negative strides? For example:
>>>打印范围(10)[: - 3:-1] [9,8]
我发现这个结果非常令人惊讶,并希望看到
规则写在某处。
>>> print range(10)[:-3:-1] [9,8]
I found this result very surprising, and would just like to see the
rules written down somewhere.
嗯,这是我尝试模拟算法。诀窍似乎是
用0或长度-1替换开始/停止参数,具体取决于步骤的
符号。
#无保修!
def指数(长度,开始,停止,步骤):
如果步骤为无:
step = 1
如果步骤< 0:
如果开始是无:
start = length-1
elif start< 0:
开始+ =长度
如果停止是无:
stop = -1
elif stop< 0:
止损+ =长度
否则:
如果开始是无:
start = 0
elif start< 0:
开始+ =长度
如果止损为无:
止损=长度
elif stop< 0:
停止+ =长度
如果开始>停止:
开始时>停止:
收益率开始
开始+ =步骤
否则:
开始时<停止:
收益率开始
开始+ =步骤
断言清单(指数(10,无,-3,-1) ))== range(10)[: - 3:-1]
断言列表(索引(10,无,-3,-2))==范围(10)[: - 3 :-2]
断言列表(指数(10,9,-3,-2))==范围(10)[9:-3:-2]
断言列表(索引(10,无,无,无))==范围(10)[::]
断言列表(索引(10,无,5,2))==范围(10)[:5:2]
我必须承认(迟到但总比没有好),Raymond Hettinger的新内搭
内置逆转( )有一些优点:
Well, here is my attempt to emulate the algorithm. The trick seems to be to
substitute the start/stop parameters with 0 or length-1 depending on the
sign of step.
# no warranties!
def indices(length, start, stop, step):
if step is None:
step = 1
if step < 0:
if start is None:
start = length-1
elif start < 0:
start += length
if stop is None:
stop = -1
elif stop < 0:
stop += length
else:
if start is None:
start = 0
elif start < 0:
start += length
if stop is None:
stop = length
elif stop < 0:
stop += length
if start > stop:
while start > stop:
yield start
start += step
else:
while start < stop:
yield start
start += step
assert list(indices(10, None, -3, -1)) == range(10)[:-3:-1]
assert list(indices(10, None, -3, -2)) == range(10)[:-3:-2]
assert list(indices(10, 9, -3, -2)) == range(10)[9:-3:-2]
assert list(indices(10, None, None, None)) == range(10)[::]
assert list(indices(10, None, 5, 2)) == range(10)[:5:2]
I have to admit (late but better than never) that Raymond Hettinger''s new
builtin reversed() has some merits:
list(reverse(range(10)[ - 2:]))== range(10)[: - 3:-1]
list(reversed(range(10)[-2:])) == range(10)[:-3:-1]
True
Peter
True
Peter
这篇关于负步幅列表切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!