Tuple index

S

Steve M

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
 
L

Larry Bates

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
 
E

Erik Max Francis

Larry said:
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.

No, that's .find in strings that returns -1. .index in lists raises a
ValueError:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.index(x): x not in list
 
J

John Machin

Steve said:
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.

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'

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'

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
?
 
S

Steve M

John said:
Steve said:
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.

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'

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'

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
?

I guess I explained my problem incorrectly. Let me try again.

tuple = ("fred", "barney", "foo")

I know that foo is an element of tuple, but what I need to know is what
the index of foo is, tuple[?]. Hopefully this explains what I'm trying
do do better. Sorry about the earlier confusion.

Steve
 
M

Michael Hartl

I actually find it strange that tuples don't have an index function,
since finding the index doesn't involve any mutation. Anyone know why
Python doesn't allow a statement like t.index('foo')?

In any case, you can use the index method of list objects if you
convert your tuple to a list first:
.... return list(a_tuple).index(element)
....
'foo'

(By the way, 'tuple' is a Python built-in type, so it's probably best
to avoid using it as a variable name.)


Michael
 
S

Steven Bethard

Steve said:
I guess I explained my problem incorrectly. Let me try again.

tuple = ("fred", "barney", "foo")

I know that foo is an element of tuple, but what I need to know is what
the index of foo is, tuple[?].

Larry Bates's solution is probably the best way to go here:

py> t = ("fred", "barney", "foo")
py> list(t).index("foo")
2
py> t[2]
'foo'

But note that if you're doing this often, you're probably using tuple
for the wrong things. Check out:

http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types

If you're iterating over the items of something and the items are all of
the same type, you probably want a list, not a tuple.

What's the use case in which you want to do this?

STeVe
 
S

Steven Bethard

Michael said:
I actually find it strange that tuples don't have an index function,
since finding the index doesn't involve any mutation. Anyone know why
Python doesn't allow a statement like t.index('foo')?

Tuples aren't really intended for this kind of use. See:

http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types

Tuples are supposed to be operated on as a group. It's even been
suggested occasionally on python-dev that in Python 3.0, tuples
shouldn't support iteration at all...

STeVe
 
S

Steve M

Steven said:
Steve said:
I guess I explained my problem incorrectly. Let me try again.

tuple = ("fred", "barney", "foo")

I know that foo is an element of tuple, but what I need to know is what
the index of foo is, tuple[?].

Larry Bates's solution is probably the best way to go here:

py> t = ("fred", "barney", "foo")
py> list(t).index("foo")
2
py> t[2]
'foo'

But note that if you're doing this often, you're probably using tuple
for the wrong things. Check out:

http://www.python.org/doc/faq general.html#why-are-there-separate-tuple-and-list-data-types

If you're iterating over the items of something and the items are all of
the same type, you probably want a list, not a tuple.

What's the use case in which you want to do this?

STeVe

I'm actually doing this as part of an exercise from a book. What the program
is supposed to do is be a word guessing game. The program automaticly
randomly selects a word from a tuple. You then have the oportunity to ask
for a hint. I created another tuple of hints, where the order of the hints
correspond to the word order. I was thinking if I could get the index
position of the randomly selected word, I pass that to the hints tuple to
display the correct hint from the hints tuple. I'm trying to do it this way
as the book I'm using has not gotten to lists yet. As you may have guessed,
I'm just learning Python. I do appreciate your help, Thank you.

Steve
 
S

Steve M

Michael said:
I actually find it strange that tuples don't have an index function,
since finding the index doesn't involve any mutation. Anyone know why
Python doesn't allow a statement like t.index('foo')?

In any case, you can use the index method of list objects if you
convert your tuple to a list first:
... return list(a_tuple).index(element)
...
'foo'

(By the way, 'tuple' is a Python built-in type, so it's probably best
to avoid using it as a variable name.)


Michael

The book I'm using to learn Python with has not gotten to lists yet, maybe
next chapter.

I knew tuple is a built in type, I was just trying to be clear, I guess I
just muddied the water a bit. Thank you for your help.

Steve
 
J

John Machin

Steve said:
I'm actually doing this as part of an exercise from a book. What the program
is supposed to do is be a word guessing game. The program automaticly
randomly selects a word from a tuple.

Care to tell us which book is using a tuple for this, but hasn't got to
lists yet?

Cheers,
John
 
S

Steven Bethard

Steve said:
I'm actually doing this as part of an exercise from a book. What the program
is supposed to do is be a word guessing game. The program automaticly
randomly selects a word from a tuple. You then have the oportunity to ask
for a hint. I created another tuple of hints, where the order of the hints
correspond to the word order. I was thinking if I could get the index
position of the randomly selected word, I pass that to the hints tuple to
display the correct hint from the hints tuple. I'm trying to do it this way
as the book I'm using has not gotten to lists yet.

I'm guessing it also hasn't gotten to dicts yet either? Perhaps a
somewhat more natural way of doing this would be something like:

py> hints = dict(word1="here's hint 1!",
.... word2="here's hint 2!",
.... word3="here's hint 3!")
py> words = list(hints)
py> import random
py> selected_word = random.choice(words)
py> selected_word
'word3'
py> print hints[selected_word]
here's hint 3!

That said, if you want to find the index of a word in a tuple without
using list methods, here are a couple of possibilities, hopefully one of
which matches the constructs you've seen so far:

py> t = ("fred", "barney", "foo")

py> for i, word in enumerate(t):
.... if word == "barney":
.... break
....
py> i
1

py> for i in range(len(t)):
.... if t == "barney":
.... break
....
py> i
1

py> i = 0
py> for word in t:
.... if word == "barney":
.... break
.... i += 1
....
py> i
1

HTH,

STeVe
 
S

Steve M

John said:
Care to tell us which book is using a tuple for this, but hasn't got to
lists yet?

Cheers,
John

Python Programming for the absoulte beginner by Michael Dawson

Steve
 
S

Steve M

Steven said:
Steve said:
I'm actually doing this as part of an exercise from a book. What the
program is supposed to do is be a word guessing game. The program
automaticly randomly selects a word from a tuple. You then have the
oportunity to ask for a hint. I created another tuple of hints, where the
order of the hints correspond to the word order. I was thinking if I
could get the index position of the randomly selected word, I pass that
to the hints tuple to display the correct hint from the hints tuple. I'm
trying to do it this way as the book I'm using has not gotten to lists
yet.

I'm guessing it also hasn't gotten to dicts yet either? Perhaps a
somewhat more natural way of doing this would be something like:

py> hints = dict(word1="here's hint 1!",
... word2="here's hint 2!",
... word3="here's hint 3!")
py> words = list(hints)
py> import random
py> selected_word = random.choice(words)
py> selected_word
'word3'
py> print hints[selected_word]
here's hint 3!

That said, if you want to find the index of a word in a tuple without
using list methods, here are a couple of possibilities, hopefully one of
which matches the constructs you've seen so far:

py> t = ("fred", "barney", "foo")

py> for i, word in enumerate(t):
... if word == "barney":
... break
...
py> i
1

py> for i in range(len(t)):
... if t == "barney":
... break
...
py> i
1

py> i = 0
py> for word in t:
... if word == "barney":
... break
... i += 1
...
py> i
1

HTH,

STeVe


Thanks Steve, I'll see if I can make that solution work for me.

Steve
 
S

Scott Robinson

In a review I found on the web:
http://www.skattabrain.com/css-books-plain/1592000738.html
"Dawson will take you by the hand and lead you down the garden path."

Malapropism? Intentional humour?

The book teaches you enough about programming and python to get you
programming. It was the only python book in my local library so I
read it while trying to learn the basics of python. It got me to the
point where I could comfortably write Fortran-style python (I am not
claiming that anyone else will leave the book that way, only that was
as far as I had progressed in understanding python).

I think that this book suggests using pickle/unpickle to send data
over the internet. This is an amazingly bad idea, which is obvious
once you understand how overloading works (I'm pretty sure overloading
isn't covered in that book).

In short, I think that this book is a good introduction to
programming, and it will explain the basics of python, but it doesn't
really begin to explain how to program in python.

Scott Robinson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,221
Messages
2,571,134
Members
47,748
Latest member
LyleMondra

Latest Threads

Top