By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Tuple index

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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:
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

Jul 18 '05 #2

P: n/a
Larry Bates wrote:
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:
[1, 2, 3].index(4)

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
Jul 18 '05 #3

P: n/a

Steve M wrote:
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
?

Jul 18 '05 #4

P: n/a
John Machin wrote:

Steve M wrote:
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
Jul 18 '05 #5

P: n/a
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:
t = ("fred", "barney", "foo")
list(t).index("foo") 2 def index(a_tuple, element): .... return list(a_tuple).index(element)
.... t[index(t, "foo")]

'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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/

Jul 18 '05 #6

P: n/a
Steve M wrote:
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/genera...ist-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
Jul 18 '05 #7

P: n/a
Michael Hartl wrote:
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/genera...ist-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
Jul 18 '05 #8

P: n/a
Steven Bethard wrote:
Steve M wrote:
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
Jul 18 '05 #9

P: n/a
Michael Hartl wrote:
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:
t = ("fred", "barney", "foo")
list(t).index("foo") 2 def index(a_tuple, element): ... return list(a_tuple).index(element)
... t[index(t, "foo")]

'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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/


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
Jul 18 '05 #10

P: n/a

Steve M wrote:
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

Jul 18 '05 #11

P: n/a
Steve M wrote:
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[i] == "barney":
.... break
....
py> i
1

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

HTH,

STeVe
Jul 18 '05 #12

P: n/a
John Machin wrote:

Steve M wrote:
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


Python Programming for the absoulte beginner by Michael Dawson

Steve
Jul 18 '05 #13

P: n/a
Steven Bethard wrote:
Steve M wrote:
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[i] == "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
Jul 18 '05 #14

P: n/a

Steve M wrote:
John Machin wrote:

Steve M wrote:
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


Python Programming for the absoulte beginner by Michael Dawson


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

Malapropism? Intentional humour?

Jul 18 '05 #15

P: n/a
On 21 Feb 2005 15:01:05 -0800, "John Machin" <sj******@lexicon.net>
wrote:

Steve M wrote:
John Machin wrote:
>
> Steve M wrote:
>> I'm actually doing this as part of an exercise from a book. Whatthe > program
>> is supposed to do is be a word guessing game. The programautomaticly >> randomly selects a word from a tuple.
>
> Care to tell us which book is using a tuple for this, but hasn'tgot to > lists yet?
>
> Cheers,
> John


Python Programming for the absoulte beginner by Michael Dawson


In a review I found on the web:
http://www.skattabrain.com/css-books...592000738.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

Jul 18 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.