index() of sequence type?

Discussion in 'Python' started by Neal Becker, Feb 7, 2008.

  1. Neal Becker

    Neal Becker Guest

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

    If not, shouldn't there be?
     
    Neal Becker, Feb 7, 2008
    #1
    1. Advertising

  2. 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
     
    Stefan Behnel, Feb 7, 2008
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, Feb 7, 2008
    #3
  4. Neal Becker

    Paul Rubin Guest

    Stefan Behnel <> writes:
    > 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.
     
    Paul Rubin, Feb 7, 2008
    #4
  5. En Thu, 07 Feb 2008 11:31:44 -0200, Diez B. Roggisch <>
    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:

    py> 2 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
     
    Gabriel Genellina, Feb 7, 2008
    #5
  6. On Feb 7, 1:57 pm, "Gabriel Genellina" <> wrote:
    > Tuples are worse: they implement
    > __contains__ but not index. So you can say:
    >
    > py> 2 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
     
    Raymond Hettinger, Feb 7, 2008
    #6
  7. En Thu, 07 Feb 2008 20:13:00 -0200, Raymond Hettinger <>
    escribió:

    > On Feb 7, 1:57 pm, "Gabriel Genellina" <> wrote:
    >> Tuples are worse: they implement
    >> __contains__ but not index. So you can say:
    >>
    >> py> 2 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
     
    Gabriel Genellina, Feb 7, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    671
    Bryce
    Mar 17, 2005
  2. Anne Wangnick
    Replies:
    1
    Views:
    326
    Peter Hansen
    Aug 31, 2004
  3. stef mientki
    Replies:
    13
    Views:
    687
    stef mientki
    Oct 20, 2007
  4. Neal Becker

    index of min element of sequence

    Neal Becker, Jan 21, 2008, in forum: Python
    Replies:
    8
    Views:
    374
    Paul Rubin
    Jan 21, 2008
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    355
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page