how to find the longst element list of lists
Dan Sommers
me at privacy.net
Mon Jan 8 02:55:19 CET 2007
On Sun, 07 Jan 2007 22:23:22 +0100,
"Michael M." <michael at mustun.ch> wrote:
> How to find the longst element list of lists?
> I think, there should be an easier way then this:
> s1 = ["q", "e", "d"]
> s2 = ["a", "b"]
> s3 = ["a", "b", "c", "d"]
[ snip ]
One more thing to think about: if your list of lists grows (i.e., if
you end up with thousands of lists instead of just three), then sorting
may not be the way to go. Assuming that list_of_lists is your list of
lists, then something like this:
longest_list, longest_length = list_of_lists[ 0 ], len( longest_list )
for a_list in list_of_lists[ 1 : ]:
a_length = len( a_list )
if a_length > longest_length:
longest_list, longest_length = a_list, a_length
will run faster than sorting the list just to pick off one element (O(n)
vs. O(n log n) for all of you Big-Oh notation fans out there; you know
who you are!).
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
More information about the Python-list
mailing list