Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > efficient matching of elements a list

Reply
Thread Tools

efficient matching of elements a list

 
 
omission9
Guest
Posts: n/a
 
      01-24-2004
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!!

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      01-24-2004
omission9 <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      01-24-2004
>>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[0]==i, A_LIST)

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

- Josiah
 
Reply With Quote
 
Gerrit Holl
Guest
Posts: n/a
 
      01-24-2004
> 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.

 
Reply With Quote
 
Rene Pijlman
Guest
Posts: n/a
 
      01-24-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Most efficient algorithm for matching Timasmith Java 36 02-16-2007 11:02 PM
the most efficient method of adding elements to the list alf Python 4 06-06-2006 08:55 AM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
print matching elements of a list starwars Perl Misc 5 02-02-2004 09:06 PM



Advertisments