efficient matching of elements a list

Discussion in 'Python' started by omission9, Jan 24, 2004.

  1. omission9

    omission9 Guest

    Suppose I have a lists of tuples
    A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
    and an int
    i=1
    What is the fastest way in python to get out all the tuples from the
    list whose first element is equal to i?
    A have a very large list and a simple
    for a in A_LIST:
    if a[0]==i:
    #We have a match!!

    Seems to be very slow and there must be some super quick pythonic way to
    do this maybe?
    Any avice would be much appreciated!!
     
    omission9, Jan 24, 2004
    #1
    1. Advertising

  2. omission9

    Paul Rubin Guest

    omission9 <> writes:

    > Suppose I have a lists of tuples
    > A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
    > and an int
    > i=1
    > What is the fastest way in python to get out all the tuples from the
    > list whose first element is equal to i?


    t = [x for x in A_LIST if x[0] == i]

    > Seems to be very slow and there must be some super quick pythonic way
    > to do this maybe?
    > Any avice would be much appreciated!!


    Maybe there's some obscure faster way to do it but if you're really
    in a hurry, use psyco or pyrex or write a C extension.
     
    Paul Rubin, Jan 24, 2004
    #2
    1. Advertising

  3. >>Suppose I have a lists of tuples
    >>A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
    >>and an int
    >>i=1
    >>What is the fastest way in python to get out all the tuples from the
    >>list whose first element is equal to i?

    >
    >
    > t = [x for x in A_LIST if x[0] == i]


    Before list comprehensions, we had to do it all with filter...
    t = filter(lambda x:x[0]==i, A_LIST)

    List comprehension seem to be around 33% faster. Long live list
    comprehensions.

    - Josiah
     
    Josiah Carlson, Jan 24, 2004
    #3
  4. omission9

    Gerrit Holl Guest

    > What is the fastest way in python to get out all the tuples from the
    > list whose first element is equal to i?


    Use a list comprehension:
    >>> L = [(1,2,3), (2,3,1), (3,2,1), (1,2,5), (5,1,3)]
    >>> [t for t in L if t[0] == 1]

    [(1, 2, 3), (1, 2, 5)]

    yours,
    Gerrit.
     
    Gerrit Holl, Jan 24, 2004
    #4
  5. omission9

    Rene Pijlman Guest

    omission9:
    >Suppose I have a lists of tuples
    >What is the fastest way in python to get out all the tuples from the
    >list whose first element is equal to i?


    A list is not an efficient data structure for this query. Why don't you
    design another data structure?

    --
    René Pijlman
     
    Rene Pijlman, Jan 24, 2004
    #5
    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. alf
    Replies:
    4
    Views:
    321
    bruno at modulix
    Jun 6, 2006
  2. Timasmith
    Replies:
    36
    Views:
    887
  3. Adam Hartshorne
    Replies:
    2
    Views:
    381
    Nitin Motgi
    Jan 27, 2006
  4. Debajit Adhikary
    Replies:
    17
    Views:
    699
    Debajit Adhikary
    Oct 18, 2007
  5. starwars

    print matching elements of a list

    starwars, Feb 1, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    109
    Michele Dondi
    Feb 2, 2004
Loading...

Share This Page