字符串负指数? [英] String negative indices?

查看:72
本文介绍了字符串负指数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Python,并且被处理负面索引的方式看起来很奇怪而感到难过。


例如,要获取最后一个字符一个字符串,我可以输入x [-1]。为了获得第二和第三,我可以输入x [-3:-1]等。这很好。


逻辑上,我应该可以输入x [-2 :-0]获取最后一个和下一个最后一个字符。但是,由于Python没有区分正负零,所以这不起作用。相反,我必须输入x [-2:]。


使用简单的常量,这没关系,但是当范围的开始和结束时,它会更烦人在变量的某个地方。我可以找到使这项工作没有大量特殊情况的唯一方法if逻辑是将负下标翻译为正数,这有点无论如何都会打败负面下标的目的。


这里有一些魔法吗?对于Python来说,将0视为特殊情况实际上不是更好吗,因此x [-2:0]和x [-2:]会产生相同的结果吗?


--Tim

解决方案

dr ** *****@comcast.net 写道:

这里有什么魔法吗?对于Python来说,将0视为特殊情况实际上不是更好吗,因此x [-2:0]和x [-2:]会产生相同的结果吗?




不,因为x [-2:0:-1]已经有意义而且它不是你想要的。

-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

心灵不是要装满的船只,而是要点火。

- 普鲁塔克


< blockquote> drtimh ... @ comcast.net写道:

我刚刚开始使用Python,并且被负面索引的处理方式看起来很奇怪所困扰。

例如,要获取字符串中的最后一个字符,我可以输入x [-1]。为了获得第二和第三,我可以输入x [-3:-1]等。这很好。

逻辑上,我应该可以输入x [-2:-0]获取最后一个和下一个最后一个字符。但是,由于Python没有区分正负零,所以这不起作用。相反,我必须输入x [-2:]。

使用简单的常量,这没关系,但是当范围的开始和结束位于某个变量时,它会更烦人。我可以找到使这项工作没有大量特殊情况的唯一方法if逻辑是将负下标转换为正数,这有点打败负面下标的目的。

这里有什么魔法吗?对于Python来说,将0视为特殊情况实际上不是更好吗,因此x [-2:0]和x [-2:]会产生相同的结果吗?

- 蒂姆




x [-2:无]


我不确定它是否符合魔法的要求但是你是对的,它不会比0更明显地比b $ b更好。


乔治


dr*******@comcast.net 写道:

从逻辑上讲,我应该可以输入x [-2:-0]来获取最后一个和下一个最后一个字符。但是,由于Python没有区分正负零,所以这不起作用。相反,我必须输入x [-2:]。




万岁!逻辑上没有正面或负面零点,

或者我是否错过了小学的东西?


PS。 x [len(x)-2:len(x)-0]

欢呼,

fw


I''m just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.

For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.

Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn''t distinguish between positive and negative zero, this doesn''t work. Instead, I have to enter x[-2:].

With simple constants, this is ok, but it''s a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.

Is there some magic I''m missing here? Wouldn''t it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?

--Tim

解决方案

dr*******@comcast.net wrote:

Is there some magic I''m missing here? Wouldn''t it actually be better for Python to treat 0 as a
special case here, so that x[-2:0] and x[-2:] generated the same result?



No, since x[-2:0:-1] already has meaning and it isn''t what you want.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The mind is not a vessel to be filled but a fire to be kindled.
-- Plutarch


drtimh...@comcast.net wrote:

I''m just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.

For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.

Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn''t distinguish between positive and negative zero, this doesn''t work. Instead, I have to enter x[-2:].

With simple constants, this is ok, but it''s a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.

Is there some magic I''m missing here? Wouldn''t it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?

--Tim



x[-2:None]

I''m not sure if it qualifies as "magic" but you''re right, it''s not more
obvious than 0 would be.

George


dr*******@comcast.net wrote:

Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn''t distinguish between positive and negative zero, this doesn''t work. Instead, I have to enter x[-2:].



Hooray! Logically there is no such thing as positive or negative zero,
or did I miss something in the primary?

PS. x[len(x)-2 : len(x)-0]

cheers,
fw


这篇关于字符串负指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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