list.index() like...but returning lists (for avoiding '0' onmultiples hits)

Discussion in 'Python' started by Srinath Avadhanula, Dec 22, 2003.

  1. > but their results is not giving me the answer that i looking for...So, here i
    > go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
    > 2.2)....There is a built-in function-method that returns [0,2]?
    >


    This works...

    >>> a = [1,2,3,4]
    >>> a[:2]

    [1, 2]
    >>>


    Srinath
     
    Srinath Avadhanula, Dec 22, 2003
    #1
    1. Advertising

  2. > but their results is not giving me the answer that i looking for...So, here i
    > go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
    > 2.2)....There is a built-in function-method that returns [0,2]?
    >


    This works...

    >>> a = [1,2,3,4]
    >>> a[:2]

    [1, 2]
    >>>


    Srinath
     
    Srinath Avadhanula, Dec 22, 2003
    #2
    1. Advertising

  3. Hi....i asume this is a veeeery usual question. I'm searching on python.org,
    but their results is not giving me the answer that i looking for...So, here i
    go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
    2.2)....There is a built-in function-method that returns [0,2]?

    Well, sory if RTFM is the answer!!

    --
    Gerardo Herzig
    Departamento de Proyectos Especiales e Internet
    Facultad de Medicina
    U.B.A.
     
    Gerardo Herzig -Departamento de Proyectos Especial, Dec 22, 2003
    #3
  4. Srinath Avadhanula

    John Roth Guest

    Re: list.index() like...but returning lists (for avoiding '0' on multiples hits)

    "Srinath Avadhanula" <> wrote in message
    news:...
    > > but their results is not giving me the answer that i looking for...So,

    here i
    > > go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
    > > 2.2)....There is a built-in function-method that returns [0,2]?
    > >

    >
    > This works...
    >
    > >>> a = [1,2,3,4]
    > >>> a[:2]

    > [1, 2]
    > >>>


    But what does that have to do with the question?

    As far as I know, there is no such built-in for lists.
    You can do something similar with the "re" module
    for strings, but not for lists.

    John Roth
    >
    > Srinath
    >
     
    John Roth, Dec 22, 2003
    #4
  5. Srinath Avadhanula

    Peter Otten Guest

    Re: list.index() like...but returning lists (for avoiding '0' on multiples hits)

    Gerardo Herzig -Departamento de Proyectos Especiales e Internet- Facultad de
    Medicina wrote:

    > Hi....i asume this is a veeeery usual question. I'm searching on
    > python.org, but their results is not giving me the answer that i looking
    > for...So, here i go...when i have myList = [2,4,2,0] ...myList.index(2)
    > returns 0 (python 2.2)....There is a built-in function-method that returns
    > [0,2]?


    I don't think so. But it's not hard to do it on your own:

    >>> def indices(iterable, value):

    .... return [index for (index, item) in enumerate(iterable) if item ==
    value]
    ....
    >>> indices([1,2,3,1,-1], 1)

    [0, 3]


    Peter
     
    Peter Otten, Dec 22, 2003
    #5
  6. Re: list.index() like...but returning lists (for avoiding '0' on multiples hits)

    John Roth wrote in message ...
    >
    >But what does that have to do with the question?
    >
    >As far as I know, there is no such built-in for lists.
    >You can do something similar with the "re" module
    >for strings, but not for lists.
    >
    >John Roth


    More and more I am finding myself wishing for a neat way to *add*
    functionality to a builtin. Not subclass a builtin, but add methods to the
    builtin type.

    Like (the function does what the OP wants, btw):

    >>> def indices(self, value):

    .... return [i for i,v in enumerate(self) if v == value]
    ....
    >>> list.indexes = indexes
    >>> [0, 2, 2, 3].indexes(2)

    [1, 2]

    This currently gives:
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    list.indexes=indexes
    TypeError: can't set attributes of built-in/extension type 'list'


    Oh well. Anyway, the following might be faster:

    def indices2(L, value):
    res = []
    last = -1 #below, last+1 to prevent infinite loop.
    try:
    while True:
    last = L.index(value, last+1)
    res.append(last)
    except ValueError:
    return res
     
    Francis Avila, Dec 23, 2003
    #6
  7. Um...my little brain came with something very much like Peter's example,
    without list comprehensions, but really close in terms of performance.

    The Francis example work well, and about 3 times faster for python 2.3, but i
    cant migrate now (im using 2.2 right now), and the extra parameter on index()
    trows exception "index() takes exactly one argument"

    I'm reading now the py.docs for that 'end' arg.

    Thank you, Peter and Francis!!!!!

    Gerardo

    El Lun 22 Dic 2003 21:07, Francis Avila escribió:
    > John Roth wrote in message ...
    >
    > >But what does that have to do with the question?
    > >
    > >As far as I know, there is no such built-in for lists.
    > >You can do something similar with the "re" module
    > >for strings, but not for lists.
    > >
    > >John Roth

    >
    > More and more I am finding myself wishing for a neat way to *add*
    > functionality to a builtin. Not subclass a builtin, but add methods to the
    > builtin type.
    >
    > Like (the function does what the OP wants, btw):
    > >>> def indices(self, value):

    >
    > ... return [i for i,v in enumerate(self) if v == value]

    ------Francis response...
    > Oh well. Anyway, the following might be faster:
    >
    > def indices2(L, value):
    > res = []
    > last = -1 #below, last+1 to prevent infinite loop.
    > try:
    > while True:
    > last = L.index(value, last+1)
    > res.append(last)
    > except ValueError:
    > return res


    --
    Gerardo Herzig
    Departamento de Proyectos Especiales e Internet
    Facultad de Medicina
    U.B.A.
     
    Gerardo Herzig -Departamento de Proyectos Especial, Dec 23, 2003
    #7
  8. Re: list.index() like...but returning lists (for avoiding '0' on multiples hits)

    On Mon, 22 Dec 2003 17:37:46 -0300, Gerardo Herzig -Departamento de
    Proyectos Especiales e Internet- Facultad de Medicina
    <> wrote:

    >Hi....i asume this is a veeeery usual question. I'm searching on python.org,
    >but their results is not giving me the answer that i looking for...So, here i
    >go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
    >2.2)....There is a built-in function-method that returns [0,2]?
    >
    >Well, sory if RTFM is the answer!!

    Do you mean a function that returns a list of the indices of all
    occurrences of the specified value? I don't think there's a builtin,
    but you could do something like this:

    def indices(lst, val):
    """ Return a list of indices of matches. """
    return [i for i in range(0, len(lst)) if lst == val]

    myList = [2, 4, 2, 0]
    print indices(myList, 2)
    # [0, 2]

    --dang
     
    Dang Griffith, Dec 23, 2003
    #8
    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. Henrik
    Replies:
    1
    Views:
    561
    Phillip Ian
    Jun 2, 2005
  2. Dos-Man
    Replies:
    5
    Views:
    1,287
    Augustus
    Mar 4, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    439
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Replies:
    11
    Views:
    569
  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