为什么Python会像* SLICING *那样做? [英] Why Python does *SLICING* the way it does??

查看:62
本文介绍了为什么Python会像* SLICING *那样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认识的很多人都在问为什么Python确实会像它那样切片.....


任何人都可以/请/给我一个很好的辩护/理由???? br />

我指的是为什么mystring [:4]给了我

元素0,1,2和3但是* NOT * mystring [4](第5版)元素)。


很多人不喜欢不邀请第5个元素。


(BTW,是的我是意识到切片

显示涉及切片_between_ elements之间的解释。这个
并不能解释为什么这是*最好的方法。)


克里斯

解决方案

>>>>> " seberino @ SPAWAR" == seberino @ spawar navy mil< se ****** @ spawar.navy.mil>写道:

我认识的很多人都在问为什么Python确实切片的方式.....
任何人/请/给我一个好的辩护/理由? ?




尝试 http://www.everything2.com/index.pl?node_id=1409551


Ganesan


-

Ganesan Rajagopal(debian.org上的rganesan)| GPG密钥:1024D / 5D8C12EA

网址: http://employees.org / ~rganesan | http://rganesan.blogspot.com


< se ****** @ spawar.navy.mil>

我认识的很多人都在问为什么Python会切割它的方式.....




半开间隔只是一种做事方式。每种方法都有自己的优点和问题。


Python的方式有一些有用的属性:


* s == s [:i] + s [i:]


* len(s [i:j])== ji#如果s足够长
OTOH,它有一些方面可以咬:


*使用s [4:2:-1]等负面步幅很尴尬。这是引入reverse()函数的主要原因。

*这会让一些人在第一次看到它时感到畏缩(显然你很明显)在那个

组)。

我怀疑它是否感觉自然取决于你以前的背景和

你是否在工作环境,数组索引为一个或来自

零。例如,C程序员习惯于看到如下代码:for(i = 0;

i< n; i ++)a [i] = f(i);相反,BASIC编程器可以用于FOR I = 1

到N:a [i] = f(I);下一个。因此,C编码器可能会发现Python的[:n]比BASIC程序员更自然。


只要语言一致关于它的方法,你只需习惯它b / b $几天后就不再是问题了。

Raymond Hettinger

Raymond Hettinger写道:

看到的代码如下:for(i = 0; i< n; i ++)a [i] = f(i);相反,
BASIC编程器可用于FOR I = 1到N:a [i] = f(I);下一步。




Afaik,至少BBC BASIC使用零基数组:-)也许ZX Spectrum

基本也是(太久以前要记住)。


-

John MexIT: http://johnbokma.com/mexit/

个人页面: http://johnbokma.com/

经验丰富的程序员: http://castleamber.com/

快乐的客户: http://castleamber.com/testimonials.html


Many people I know ask why Python does slicing the way it does.....

Can anyone /please/ give me a good defense/justification???

I''m referring to why mystring[:4] gives me
elements 0, 1, 2 and 3 but *NOT* mystring[4] (5th element).

Many people don''t like idea that 5th element is not invited.

(BTW, yes I''m aware of the explanation where slicing
is shown to involve slices _between_ elements. This
doesn''t explain why this is *best* way to do it.)

Chris

解决方案

>>>>> "seberino@spawar" == seberino@spawar navy mil <se******@spawar.navy.mil> writes:

Many people I know ask why Python does slicing the way it does.....
Can anyone /please/ give me a good defense/justification???



Try http://www.everything2.com/index.pl?node_id=1409551

Ganesan

--
Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
Web: http://employees.org/~rganesan | http://rganesan.blogspot.com


<se******@spawar.navy.mil>

Many people I know ask why Python does slicing the way it does.....



Half open intervals are just one way of doing things. Each approach has its own
merits and issues.

Python''s way has some useful properties:

* s == s[:i] + s[i:]

* len(s[i:j]) == j-i # if s is long enough
OTOH, it has some aspects that bite:

* It is awkward with negative strides such as with s[4:2:-1]. This was the
principal reason for introducing the reversed() function.

* It makes some people cringe when they first see it (you''re obviously in that
group).
I suspect that whether it feels natural depends on your previous background and
whether you''re working in an environment with arrays indexed from one or from
zero. For instance, C programmers are used to seeing code like: for(i=0 ;
i<n; i++) a[i]=f(i); In contrast, a BASIC programmer may be used to FOR I = 1
to N: a[i]=f(I); NEXT. Hence, the C coders may find Python''s a[:n] to be
more natural than BASIC programmers.

As long as a language is consistent about its approach, you just get used to it
and it stops being an issue after a few days.
Raymond Hettinger


Raymond Hettinger wrote:

to seeing code like: for(i=0 ; i<n; i++) a[i]=f(i); In contrast, a
BASIC programmer may be used to FOR I = 1 to N: a[i]=f(I); NEXT.



Afaik, at least BBC BASIC uses zero based arrays :-) Maybe ZX Spectrum
Basic too (too long ago to remember).

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html


这篇关于为什么Python会像* SLICING *那样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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