元组索引 [英] Tuple index

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

问题描述

您好,


我想弄清楚元组成员的索引位置。

我知道会员名称,但我需要了解会员指数的位置。我知道如果我使用声明print tuple [4]它会打印该位置的

内容。我不明白的是,如果我知道foo是
a元组成员,我怎么能得到foo的索引位置。

谢谢你提前

Steve

Hello,

I''m trying to figure out the index position of a tuple member.
I know the member name, but I need to know the members index position. I
know that if I use the statement print tuple[4] that it will print the
contents of that location. What I don''t understand is if I know that foo is
a member of tuple, how do I get foo''s index position.
Thanks-in-Advance
Steve

推荐答案

元组没有列出的所有好方法

所以将它转换成一个列表。


元组=('''',''b'',''c'',''d'')
l = list(tuple)


现在你可以这样做:


list.index(''c'')


返回2


记住索引返回-1时没有找到。


拉里贝茨

Steve M写道:
Tuples don''t have all the nice methods that lists have
so convert it to a list.

tuple=(''a'',''b'',''c'',''d'')
l=list(tuple)

now you can do:

list.index(''c'')

which returns 2

Remember index returns -1 when nothing is found.

Larry Bates
Steve M wrote:
你好,

我正在试图找出一个元组成员的索引位置。
我知道会员名称,但我需要知道会员索引的位置。我知道如果我使用语句print tuple [4]它将打印该位置的
内容。我不明白的是,如果我知道foo是元组成员,我怎么能得到foo的索引位置。
感谢提前
Steve
Hello,

I''m trying to figure out the index position of a tuple member.
I know the member name, but I need to know the members index position. I
know that if I use the statement print tuple[4] that it will print the
contents of that location. What I don''t understand is if I know that foo is
a member of tuple, how do I get foo''s index position.
Thanks-in-Advance
Steve



Larry Bates写道:
Larry Bates wrote:
元组没有列出的所有好方法
所以将它转换成一个列表。

tuple =('''',''b'',''c'',''d'')
l = list( tuple)

现在你可以这样做:

list.index(''c'')

返回2
Tuples don''t have all the nice methods that lists have
so convert it to a list.

tuple=(''a'',''b'',''c'',''d'')
l=list(tuple)

now you can do:

list.index(''c'')

which returns 2

Remember index returns -1 when nothing is found.




不,那是返回-1的字符串中的.find。列表中的.index引发了一个

ValueError:



No, that''s .find in strings that returns -1. .index in lists raises a
ValueError:

[1,2,3 ] .index(4)
[1, 2, 3].index(4)



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

文件"< stdin>" ;,第1行,在?

ValueError:list.index(x):x不在列表中


-

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

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

地图不是领土。

- Alfred Korzybski


Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.index(x): x not in list

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The map is not the territory.
-- Alfred Korzybski




Steve M写道:

Steve M wrote:
你好,

我正在试图找出元组
成员的索引位置。我知道会员名称,但我需要知道成员索引
的位置。


元组,就像列表一样,没有成员,因为他们可以成为

" named"喜欢t.foo。引用它们的唯一方法是通过索引,

t [4]。

我知道如果我使用语句print tuple [4]那么它将打印
该位置的内容。我不明白的是,如果我知道
foo是元组的成员,我如何得到foo的索引位置。
Hello,

I''m trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position.

Tuples, like lists, don''t have members in the sense that they can be
"named" like t.foo. The only way of referring to them is by index,
t[4].
I
know that if I use the statement print tuple[4] that it will print the contents of that location. What I don''t understand is if I know that foo is a member of tuple, how do I get foo''s index position.




你*不能*知道foo是元组的成员。


考虑一下:



You *can''t* "know that foo is a member of tuple".

Consider this:

foo =''carol''
t =(123,456,789,''bob'',foo,'ted'')
t [4]
''carol''


这就是你所说的foo是t的成员?嗯,事实并非如此。 foo是

对字符串''carol''的引用。 t [4]也是对

字符串''carol''的引用。


现在继续阅读...

foo =''alice''
t
(123,456,789,''bob'',''carol'',''ted'')t [4]
''carol''
foo = ''carol''
t = (123,456,789,''bob'',foo,''ted'')
t[4] ''carol''

Is that what you mean by "foo is a member of t''? Well, it''s not. foo is
a reference to the string ''carol''. t[4] is also a reference to the
string ''carol''.

Now read on ...
foo = ''alice''
t (123, 456, 789, ''bob'', ''carol'', ''ted'') t[4] ''carol''




现在foo是对字符串''alice''的引用。无论是之前还是现在都与t无关。


您是否阅读过 http://docs.python.org/tut/tut.html





Now foo is a reference to the string ''alice''. Nothing to do with t,
either before or now.

Have you read the tutorial found at http://docs.python.org/tut/tut.html
?


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

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