"指数"方法只适用于可变序列? [英] "index" method only for mutable sequences??

查看:51
本文介绍了"指数"方法只适用于可变序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找能够将索引返回到列表中第一个

匹配元素的函数或方法。来自C ++ STL背景,我认为它可能会被称为查找。我的第一站是图书馆的序列类型页面

参考( http://docs.python.org/lib/typesseq.html); 它不存在。一个搜索

的库参考索引似乎证实该函数没有

存在。过了一会儿,我意识到它可能被称为索引。代替。 Voila。


我的观点是文档列出并将其描述为仅存在于

MUTABLE序列的方法。为什么只为变异?我期望它覆盖的对象类别将是所有有序序列,或者,更多地说明一点点

有针对性地支持有序INDEXing的任何东西。我的理解是,

dict'不属于那类对象,因为他们的订购不是记录或依赖的b $ b。然而,元组's确实支持有序索引,

所以为什么不用元组'有一个索引方法?


PS:我知道我还没有得到答案我的为什么问题,但是,

假设它只是一个疏忽或一个没有大图的设计的例子

考虑到,修复这种疏忽的额外好处将是可以将索引

方法的文档从当前奇怪的看似位置移动到可变序列类型上。页面到某个地方有人会在逻辑上寻找它。


PPS:尽管我的问题的基本性质看起来很像,这个

isn'我第一天使用Python。我已经使用了它好几年了,而且我需要付出很多钱。这只是因为我对语言的热爱,我质疑它的方式,所以当我猜这个原因时,请不要过于防守

监督是缺乏设计。


Corey Lubin

I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn''t there. A search
of the Library Reference''s index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict''s don''t fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple''s do support ordered indexing,
so why don''t tuple''s have an index method?

P.S.: I know I haven''t yet gotten an answer to my "why" question yet, but,
assuming it''s just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method''s documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn''t my first day using Python. I''ve used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don''t be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin

推荐答案



CL写道:

C.L. wrote:

我正在寻找一个函数或方法,将索引返回到列表中的第一个

匹配元素。来自C ++ STL背景,我认为它可能会被称为查找。我的第一站是图书馆的序列类型页面

参考( http://docs.python.org/lib/typesseq.html); 它不存在。一个搜索

的库参考索引似乎证实该函数没有

存在。过了一会儿,我意识到它可能被称为索引。代替。 Voila。


我的观点是文档列出并将其描述为仅存在于

MUTABLE序列的方法。为什么只为变异?我期望它覆盖的对象类别将是所有有序序列,或者,更多地说明一点点

有针对性地支持有序INDEXing的任何东西。我的理解是,

dict'不属于那类对象,因为他们的订购不是记录或依赖的b $ b。然而,元组's确实支持有序索引,

所以为什么不用元组'有一个索引方法?


PS:我知道我还没有得到答案我的为什么问题,但是,

假设它只是一个疏忽或一个没有大图的设计的例子

考虑到,修复这种疏忽的额外好处将是可以将索引

方法的文档从当前奇怪的看似位置移动到可变序列类型上。页面到某个地方有人会在逻辑上寻找它。


PPS:尽管我的问题的基本性质看起来很像,这个

isn'我第一天使用Python。我已经使用了它好几年了,而且我需要付出很多钱。这只是因为我对语言的热爱,我质疑它的方式,所以当我猜这个原因时,请不要过于防守

监督是缺乏设计。


Corey Lubin
I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn''t there. A search
of the Library Reference''s index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict''s don''t fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple''s do support ordered indexing,
so why don''t tuple''s have an index method?

P.S.: I know I haven''t yet gotten an answer to my "why" question yet, but,
assuming it''s just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method''s documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn''t my first day using Python. I''ve used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don''t be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin



环顾谷歌一点点,人们一直在问这个问题

问题至少从1992年开始。这是BDFL所说的:


Guido van Rossum(Gu **********) **** @ cwi.nl)

Wed,04 Dec 91 18:48:34 +0100

Looking around google a little bit, people have been asking that same
questions since at least 1992. Here is what the BDFL has to say:

Guido van Rossum (Gu**************@cwi.nl)
Wed, 04 Dec 91 18:48:34 +0100


>在回复:Steven D. Majewski:为什么没有元组或字符串的索引?

大多数操作可变序列而不是不可变序列的函数显然都存在,因为它们是DO改变顺序。
但是:为什么没有string.index()或tuple.index()?

这只是一个疏忽吗?
如果没有,是什么原因?
>In reply to: Steven D. Majewski: "Why no index for tuples or strings ?"

Most of the functions that operate on mutable sequences but NOT on
immutable ones are obviously there because they DO CHANGE the sequence.
BUT: why no string.index() or tuple.index() ?

Is this just an oversight ?
If not, what is the reason?



嗯,这不是一个真正的理由。我在

防御中可以说的一件事是字符串和元组对象完全没有方法,所有这些操作都是通过内置操作完成的,例如 + QUOT;和

" [...]",所以添加一个索引方法将是

结构的一点变化。


对于元组,我怀疑很少会使用这样的函数;我认为

大多数情况下x.index()会很有用,x通常是一个

列表,其内容随时间而变化,而不是元组(其中不能轻易改变



对于字符串,有一个内置模块string它导出一个

函数index它搜索子串,所以你可以说


string.index(''一二三'',''两'')


- Guido van Rossum,CWI,阿姆斯特丹< gu *** @ cwi.nl>

Umm, there isn''t a real good reason. One thing I can say in my
defense is that string and tuple objects have no methods at all, all
operations on these are done with built-in operations like "+" and
"[...]", so adding an "index" method would be a bit of a change in the
structure.

For tuples, I suspect such a function would rarely be used; I think
that is most cases where x.index() would be useful, x is generally a
list, whose contents varies in time, rather than a tuple (which cannot
change easily).

For strings, there is a built-in module "string" which exports a
function "index" which searches for substrings, so you can say

string.index(''one two three'', ''two'')

--Guido van Rossum, CWI, Amsterdam <gu***@cwi.nl>


CL写道:
C.L. wrote:

我正在寻找一个函数或方法,将索引返回到列表中的第一个

匹配元素。来自C ++ STL背景,我认为它可能会被称为查找。我的第一站是图书馆的序列类型页面

参考( http://docs.python.org/lib/typesseq.html); 它不存在。一个搜索

的库参考索引似乎证实该函数没有

存在。过了一会儿,我意识到它可能被称为索引。代替。 Voila。


我的观点是文档列出并将其描述为仅存在于

MUTABLE序列的方法。为什么只为变异?我期望它覆盖的对象类别将是所有有序序列,或者,更多地说明一点点

有针对性地支持有序INDEXing的任何东西。我的理解是,

dict'不属于那类对象,因为他们的订购不是记录或依赖的b $ b。然而,元组's确实支持有序索引,

所以为什么不用元组'有一个索引方法?


PS:我知道我还没有得到答案我的为什么问题,但是,

假设它只是一个疏忽或一个没有大图的设计的例子

考虑到,修复这种疏忽的额外好处将是可以将索引

方法的文档从当前奇怪的看似位置移动到可变序列类型上。页面到某个地方有人会在逻辑上寻找它。


PPS:尽管我的问题的基本性质看起来很像,这个

isn'我第一天使用Python。我已经使用了它好几年了,而且我需要付出很多钱。这只是因为我对语言的热爱,我质疑它的方式,所以当我猜这个原因时,请不要过于防守

监督是缺乏设计。


Corey Lubin

I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn''t there. A search
of the Library Reference''s index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict''s don''t fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple''s do support ordered indexing,
so why don''t tuple''s have an index method?

P.S.: I know I haven''t yet gotten an answer to my "why" question yet, but,
assuming it''s just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method''s documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn''t my first day using Python. I''ve used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don''t be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin



为确保设计决策而浪费的打字数量比如这可以让b / b
boggle一个人的想法。只要使用列表,除非你有压倒性的理由

否则。


James

The amount of typing wasted to defend design decisions such as this can
boggle one''s mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James


James Stroud< jstroud< atmbi.ucla.eduwrites:
James Stroud <jstroud <atmbi.ucla.eduwrites:

>

CL写道:
>
C.L. wrote:

我正在寻找一个函数或方法,它将索引返回到列表中的第一个匹配元素

。 ...

... __请不要过度防守_ ...
I was looking for a function or method that would return the index to the
first matching element in a list. ...
... __please don''t be overly defensive__ ...



浪费的打字数量来捍卫设计决策比如这可以让b / b
boggle一个人的想法。只要使用清单,除非你有压倒性的理由

否则。


詹姆斯


The amount of typing wasted to defend design decisions such as this can
boggle one''s mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James



阅读报价。我*是*使用清单。


这并没有改变这是不友好的设计这一事实。这是一个丑陋的,不一致的Python过去的大块,其中内置类型的行为不像

对象。它像拇指一样伸出来,可能只是不常见。


哦,感谢您对预期反应的侮辱性语气。你有没有更好地处理你的时间而不是浪费字节写空的回复

到你认为浪费打字的东西?


*叹气*就像我预期的那样:另一个空闲的巨魔为了保卫它而捍卫一些东西。另一方面,感谢7stud,真正有用的

响应。


Read the quote. I *am* using a list.

That doesn''t change the fact that this is unfriendly design. It''s an ugly
inconsistent chunk of a Python''s past in which built-in types didn''t behave like
objects. It sticks out like a sore thumb, maybe just not very often.

Oh, and thanks for the insulting tone of your anticipated response. Have you
anything better to do with your time than wasting bytes writing empty responses
to what you already deem a waste of typing?

*sighs* just what I expected: another idle troll defending something just for
the sake of defending it. On the other hand, thanks 7stud, for the truly helpful
response.


这篇关于&QUOT;指数&QUOT;方法只适用于可变序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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