元组问题 [英] Tuple question

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

问题描述




为什么元组没有方法''count''和''index''?它似乎
似乎它们可以存在于一个不可变的对象上。


我意识到它很容易将元组转换为一个列表并执行此操作,

我只是好奇为什么它是必要的..

谢谢,


将McGugan

Hi,

Why is that a tuple doesnt have the methods ''count'' and ''index''? It
seems they could be present on a immutable object.

I realise its easy enough to convert the tuple to a list and do this,
I''m just curious why it is neccesary..
Thanks,

Will McGugan

推荐答案

我不确定你的意思是什么。但是你可以使用len()来获取元组中

的对象数。例如
I''m not sure what do you mean by index. But you can use len() to get the
number of objects in a tuple. e.g.
t =(1,2,3)
len(t)
3 t [2]
3
t=(1,2,3)
len(t) 3 t[2] 3




为什么元组没有方法''count''和''index ''?它似乎可以出现在一个不可变的对象上。

我意识到它很容易将元组转换成一个列表并执行此操作,
我只是好奇为什么这是必要的..

谢谢,

Will McGugan

Hi,

Why is that a tuple doesnt have the methods ''count'' and ''index''? It
seems they could be present on a immutable object.

I realise its easy enough to convert the tuple to a list and do this,
I''m just curious why it is neccesary..
Thanks,

Will McGugan






伟业董建华写道:
Wai Yip Tung wrote:
我不确定索引是什么意思。但是你可以使用len()来获取元组中对象的数量。例如
I''m not sure what do you mean by index. But you can use len() to get
the number of objects in a tuple. e.g.
t =(1,2,3)
len(t)
3
t [2 ]
3
t=(1,2,3)
len(t)
3
t[2]
3




Lista有一个索引方法,它返回元素的第一次出现的索引

,但是元组不是(也不算)。只是想知道为什么。



Lista have an index method that returns the index of the first occurance
of an element, but tuple doesnt (nor count). Just wondering why.

l = [1,2,3]
t =(1,2,3)
l.index(2)
1 t.index(2)
l= [ 1, 2, 3 ]
t= ( 1, 2, 3 )
l.index(2) 1 t.index(2)



回溯(最近一次调用最后一次):

文件"< pyshell#8>",第1行,在?

t.index(2)

AttributeError:''tuple''对象没有属性''index''


Traceback (most recent call last):
File "<pyshell#8>", line 1, in ?
t.index(2)
AttributeError: ''tuple'' object has no attribute ''index''


哎呀我误解了你说的计数和索引。现在我明白了。


作为Python的用户,这是我的看法:


你认为元组是一个不可变的列表版本。但是在Python的设计中,他们有不同的目的。列出同类商品的集合,而

元组是任何类型商品的方便分组。例如,你可以用这样的方式使用它们:


users = [''admin'',''user1'',''user2'' ]

地址=('''www.python.org'',80)


索引和计数只有在集合是同质的时候才有意义。 />
因此它们没有为元组定义。


tung


2004年9月2日星期四17:40:27 + 0100,Will McGugan

< ne ** @ NOwillmcguganSPAM.com>写道:
Oops I misunderstood that you said about count and index. Now I got it.

Speaking as a user of Python, here is my take:

You consider tuple an immutable version of list. But in Python''s design
they have different purpose. List a collection of homogeneous items, while
tuple is a convenient grouping of any kind of items. For example, you can
use them this way:

users = [''admin'', ''user1'', ''user2'']
address = (''www.python.org'', 80)

index and count only make sense when the collection is homogeneous.
Therefore they are not defined for tuple.

tung

On Thu, 02 Sep 2004 17:40:27 +0100, Will McGugan
<ne**@NOwillmcguganSPAM.com> wrote:
Wai Yip Tung写道:
Wai Yip Tung wrote:
我不知道索引是什么意思。但是你可以使用len()来获取元组中对象的数量。例如
I''m not sure what do you mean by index. But you can use len() to get
the number of objects in a tuple. e.g.
> t =(1,2,3)
> len(t)
> t=(1,2,3)
> len(t)


3


3

> t [2]
> t[2]


3



Lista有一个索引方法,它返回元素第一次出现的索引,但是元组不是(也不算)。只是想知道为什么。



Lista have an index method that returns the index of the first occurance
of an element, but tuple doesnt (nor count). Just wondering why.

>>> l = [1,2,3]
>>> t =(1,2,3)
>>> l.index(2)1>>> t.index(2)
>>> l= [ 1, 2, 3 ]
>>> t= ( 1, 2, 3 )
>>> l.index(2) 1 >>> t.index(2)


Traceback(最近一次呼叫最后一次):
文件"< pyshell#8>",第1行,在? t.index(2)
AttributeError:''tuple''对象没有属性''index''


Traceback (most recent call last):
File "<pyshell#8>", line 1, in ?
t.index(2)
AttributeError: ''tuple'' object has no attribute ''index''






这篇关于元组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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