Find Items & Indices In A List...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrea.gavana@agip.it

    Find Items & Indices In A List...

    Hello NG,

    I was wondering if there is a faster/nicer method (than a for loop)
    that will allow me to find the elements (AND their indices) in a list that
    verify a certain condition. For example, assuming that I have a list like:

    mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]

    I would like to find the indices of the elements in the list that are equal
    to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
    use a for loop but I was wondering if there is a faster method...

    Thanks for every suggestion.

    Andrea.
    ------------------------------------------------------------------------------------------------------------------------------------------
    Message for the recipient only, if received in error, please notify the
    sender and read http://www.eni.it/disclaimer/


  • kaerbuhez

    #2
    Re: Find Items & Indices In A List...

    <andrea.gavana@ agip.it> a écrit dans le message de news:
    mailman.7461.11 02691050.5135.p ython-list@python.org...[color=blue]
    > For example, assuming that I have a list like:
    >
    > mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
    >
    > I would like to find the indices of the elements in the list that are
    > equal
    > to 1 (in this case, the 1,2,3,4,9 elements are equal to 1).[/color]

    List comprehension is your friend:

    PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
    win32.
    Portions Copyright 1994-2004 Mark Hammond (mhammond@skipp inet.com.au) - see
    'Help/About PythonWin' for further copyright information.[color=blue][color=green][color=darkred]
    >>> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
    >>> [i for i, j in enumerate(mylis t) if j==1][/color][/color][/color]
    [1, 2, 3, 4, 9][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Ola Natvig

      #3
      Re: Find Items &amp; Indices In A List...

      andrea.gavana@a gip.it wrote:[color=blue]
      > Hello NG,
      >
      > I was wondering if there is a faster/nicer method (than a for loop)
      > that will allow me to find the elements (AND their indices) in a list that
      > verify a certain condition. For example, assuming that I have a list like:
      >
      > mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
      >
      > I would like to find the indices of the elements in the list that are equal
      > to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
      > use a for loop but I was wondering if there is a faster method...
      >
      > Thanks for every suggestion.
      >
      > Andrea.
      > ------------------------------------------------------------------------------------------------------------------------------------------
      > Message for the recipient only, if received in error, please notify the
      > sender and read http://www.eni.it/disclaimer/
      >
      >[/color]

      You could do a list comprehension /generator expression. Like this:
      [i for i in range(len(mylis t)) if mylist[i] == 1]

      --
      --------------------------------------
      Ola Natvig <ola.natvig@inf osense.no>
      infoSense AS / development

      Comment

      • Bengt Richter

        #4
        Re: Find Items &amp; Indices In A List...

        On Fri, 10 Dec 2004 16:01:26 +0100, andrea.gavana@a gip.it wrote:
        [color=blue]
        >Hello NG,
        >
        > I was wondering if there is a faster/nicer method (than a for loop)
        >that will allow me to find the elements (AND their indices) in a list that
        >verify a certain condition. For example, assuming that I have a list like:
        >
        >mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
        >
        >I would like to find the indices of the elements in the list that are equal
        >to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
        >use a for loop but I was wondering if there is a faster method...
        >
        >Thanks for every suggestion.
        >[/color]
        One way:
        [color=blue][color=green][color=darkred]
        >>> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
        >>> [i for i,v in enumerate(mylis t) if v==1][/color][/color][/color]
        [1, 2, 3, 4, 9]

        Regards,
        Bengt Richter

        Comment

        • Steven Bethard

          #5
          Re: Find Items &amp; Indices In A List...

          andrea.gavana@a gip.it wrote:[color=blue]
          > Hello NG,
          >
          > I was wondering if there is a faster/nicer method (than a for loop)
          > that will allow me to find the elements (AND their indices) in a list that
          > verify a certain condition. For example, assuming that I have a list like:
          >
          > mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
          >
          > I would like to find the indices of the elements in the list that are equal
          > to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
          > use a for loop but I was wondering if there is a faster method...[/color]

          Everyone has already given you the answer (enumerate in a LC or GE), I'd
          just comment that it's easy enough to extend their answers to any given
          condition:
          [color=blue][color=green][color=darkred]
          >>> def getindices(sequ ence, predicate):[/color][/color][/color]
          .... return [i for i, v in enumerate(seque nce) if predicate(v)]
          ....[color=blue][color=green][color=darkred]
          >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], bool)[/color][/color][/color]
          [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][color=blue][color=green][color=darkred]
          >>> def equalsone(v):[/color][/color][/color]
          .... return v == 1
          ....[color=blue][color=green][color=darkred]
          >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], equalsone)[/color][/color][/color]
          [1, 2, 3, 4, 9][color=blue][color=green][color=darkred]
          >>> def f(v):[/color][/color][/color]
          .... return pow(v, 3, 4) == 3
          ....[color=blue][color=green][color=darkred]
          >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], f)[/color][/color][/color]
          [7]

          Steve

          Comment

          • Bengt Richter

            #6
            Re: Find Items &amp; Indices In A List...

            On Fri, 10 Dec 2004 16:27:29 GMT, Steven Bethard <steven.bethard @gmail.com> wrote:
            [color=blue]
            >andrea.gavana@ agip.it wrote:[color=green]
            >> Hello NG,
            >>
            >> I was wondering if there is a faster/nicer method (than a for loop)
            >> that will allow me to find the elements (AND their indices) in a list that
            >> verify a certain condition. For example, assuming that I have a list like:
            >>
            >> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
            >>
            >> I would like to find the indices of the elements in the list that are equal
            >> to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
            >> use a for loop but I was wondering if there is a faster method...[/color]
            >
            >Everyone has already given you the answer (enumerate in a LC or GE), I'd
            >just comment that it's easy enough to extend their answers to any given
            >condition:
            >[color=green][color=darkred]
            > >>> def getindices(sequ ence, predicate):[/color][/color]
            >... return [i for i, v in enumerate(seque nce) if predicate(v)]
            >...[color=green][color=darkred]
            > >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], bool)[/color][/color]
            >[1, 2, 3, 4, 5, 6, 7, 8, 9, 10][color=green][color=darkred]
            > >>> def equalsone(v):[/color][/color]
            >... return v == 1
            >...[color=green][color=darkred]
            > >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], equalsone)[/color][/color]
            >[1, 2, 3, 4, 9][color=green][color=darkred]
            > >>> def f(v):[/color][/color]
            >... return pow(v, 3, 4) == 3
            >...[color=green][color=darkred]
            > >>> getindices([0,1,1,1,1,5,6,7 ,8,1,10], f)[/color][/color]
            >[7]
            >[/color]
            Conclusion:
            Python is programmer's Lego ;-)

            Regards,
            Bengt Richter

            Comment

            Working...