这两个陈述有什么区别? [英] What's the difference between these 2 statements?

查看:62
本文介绍了这两个陈述有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个陈述有什么区别?


如果你有一个String s =" 12345"


s [ len(s):: - 1] =" 54321"





s [len(s):0:-1] =" 5432"


为什么?有什么不同?如果我要提供所有3个参数,那么我可以用什么数字作为切片的结尾?

谢谢,

AT

What''s the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What''s the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?
Thanks,
AT

推荐答案

ATSkyWalker写道:
ATSkyWalker wrote:
这两个陈述有什么区别?

如果你有一个String s =" 12345"

s [len(s):: - 1] =" 54321"



> s [len(s):0:-1] =" 5432"

为什么?有什么不同?如果我要提供所有3个参数,那么我可以使用哪个数字作为切片的结尾?
What''s the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What''s the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?




-1。


Reinhold



-1.

Reinhold


Reinhold Birkenfeld写道:
Reinhold Birkenfeld wrote:
ATSkyWalker写道:
ATSkyWalker wrote:
什么这两个陈述之间的区别是什么?

如果你有一个String s =" 12345"

s [len(s):: - 1] =" ; 54321

但是

s [len(s):0:-1] =" 5432"

为什么?有什么不同?如果我要提供所有3个参数,那么我可以使用哪个数字作为切片的结尾?
What''s the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What''s the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?



-1。


-1.




-len(s)或更少。

-1将返回一个空字符串。


实际上你是从len开始的-1(len(s)不是s中的索引)当你到达指定的索引(或结束)时,你

停止。因为-1是与起始者相同的指数(-1~> len(s)-1,-2~> len(s)-2,

-len(s)+ 1~> 0),你最后得到一个空字符串。


因此你必须尝试降低指数(由于负面

step)比列表的最小有效索引要完全反转

完全。



-len(s) or less.
-1 will return an empty string.

Actually you start from len(s)-1 (len(s) is not an index in s) and you
stop when you reach the index specified (or the end). Since -1 is the
same index as the starting one (-1~>len(s)-1, -2~>len(s)-2,
-len(s)+1~>0), you end up with an empty string.

Therefore you have to try to reach indices lower (due to the negative
step) than the minimum valid index of your list in order to reverse it
fully.


s [len (s): - 1:-1]产生一个空列表!


测试代码:


s =" 12345"

print s [len(s):: - 1] - >打印54321

print s [len(s): - 1:-1] - >打印" (没有)

s[len(s):-1:-1] yields an empty list !

Test code :

s = "12345"
print s[len(s)::-1] -> prints "54321"
print s[len(s):-1:-1] -> prints "" (nothing)


这篇关于这两个陈述有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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