473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

index() of sequence type?

I see list has index member, but is there an index function that applies to
any sequence type?

If not, shouldn't there be?

Feb 7 '08 #1
6 1208
Neal Becker wrote:
I see list has index member, but is there an index function that applies to
any sequence type?
Like this?

def find_index(seq, value):
try:
find_index = seq.index
except AttributeError:
def find_index(value):
for i,v in enumerate(seq):
if v == value: return i
raise ValueError("index(seq, x): x not in sequence")
return find_index(value)

If not, shouldn't there be?
I don't see the need.

Stefan
Feb 7 '08 #2
Neal Becker wrote:
I see list has index member, but is there an index function that applies
to any sequence type?

If not, shouldn't there be?
Looks like an oversight to me as well, yes. The only "difficult"
implementation would be the one for xrange, because you can't search but
must compute the result - but that should be trivial.

Diez
Feb 7 '08 #3
Stefan Behnel <st*******@behnel.dewrites:
def find_index(seq, value):
try:
find_index = seq.index
except AttributeError:
def find_index(value):
for i,v in enumerate(seq):
if v == value: return i
raise ValueError("index(seq, x): x not in sequence")
return find_index(value)
It doesn't seem like a great idea to do operations like that on
mutable iterators. But if you must:

from itertools import dropwhile
def find_index(seq, value):
a = dropwhile (lambda x: x[1] != value, enumerate(seq))
return a.next()[0]

seems more direct. I think it will raises StopIteration if the value
is not found.
Feb 7 '08 #4
En Thu, 07 Feb 2008 11:31:44 -0200, Diez B. Roggisch <de***@nospam.web.de>
escribió:
>I see list has index member, but is there an index function that applies
to any sequence type?

If not, shouldn't there be?

Looks like an oversight to me as well, yes. The only "difficult"
implementation would be the one for xrange, because you can't search but
must compute the result - but that should be trivial.
xrange is iterable, but not a sequence. Tuples are worse: they implement
__contains__ but not index. So you can say:

py2 in (1,2,4,8)
True

but not:

py(1,2,4,8).index(2)

Given that to implement __contains__ it has to scan the values the same
way as index would do, it's like a tuple saying: "I know where that item
is, and you know that I know that, but I won't tell you!" - rather
frustrating.

--
Gabriel Genellina

Feb 7 '08 #5
On Feb 7, 1:57 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
Tuples are worse: they implement
__contains__ but not index. So you can say:

py2 in (1,2,4,8)
True

but not:

py(1,2,4,8).index(2)
You must be using an old version of Python like 2.5 ;-)

As of yesterday, Py2.6 has tuple.index() and tuple.count().

Python 2.6a0 (trunk:60638M, Feb 6 2008, 18:10:45)
[GCC 4.1.1 (Gentoo 4.1.1)] on linux2
>>(1,2,4,8).index(2)
1
Raymond

Feb 7 '08 #6
En Thu, 07 Feb 2008 20:13:00 -0200, Raymond Hettinger <py****@rcn.com>
escribió:
On Feb 7, 1:57 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
>Tuples are worse: they implement
__contains__ but not index. So you can say:

py2 in (1,2,4,8)
True

but not:

py(1,2,4,8).index(2)

You must be using an old version of Python like 2.5 ;-)

As of yesterday, Py2.6 has tuple.index() and tuple.count().

Python 2.6a0 (trunk:60638M, Feb 6 2008, 18:10:45)
[GCC 4.1.1 (Gentoo 4.1.1)] on linux2
>>(1,2,4,8).index(2)
1
The Time Machine in action again!

--
Gabriel Genellina

Feb 7 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
3
by: Eric Davies | last post by:
We've implemented a 5D box data type and have implemented both RTree and GiST access methods under PostgresSQL 7.4 and PostgresSQL 7.4.1. The 5D box internally looks like: struct Box5D{ float...
12
by: bernhard.willems | last post by:
We are using DB2 UDB version 8.1 fp7 & fp9 on AIX 5.2. On our databases we do massive inserts and deletes (millions of rows). The performance is dramatically reduced because of pseudo deleted keys....
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
30
by: Eric Lilja | last post by:
Hello, I have a function that has a static array with two elements in it. When the function is called the first time, it should access element 0, the second time it should access element 1, the...
35
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
122
by: C.L. | last post by:
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...
96
by: zzbbaadd | last post by:
What's with the index() function of lists throwing an exception on not found? Let's hope this is rectified in Python 3. If nothing else, add a function that doesn't throw an exception. There are a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.