Velocity Reviews > how to find the longst element list of lists

# how to find the longst element list of lists

Michael M.
Guest
Posts: n/a

 01-07-2007
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"]

if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist längster
if len(s2) >= len(s3):
sx2=s2
sx3=s3
else:
sx2=s3
sx3=s2

if len(s2) >= len(s3) and len(s2) >= len(s1):
sx1=s2 ## s2 ist längster
if len(s3) >= len(s1):
sx2=s3
sx3=s1
else:
sx2=s1
sx3=s3

if len(s3) >= len(s1) and len(s3) >= len(s2):
sx1=s3 ## s3 ist längster
if len(s1) >= len(s2):
sx2=s1
sx3=s2
else:
sx2=s2
sx3=s1

After, the list ist sorted:

sx1 = ["a", "b", "c", "d"]
sx2 = ["q", "e", "d"]
sx3 = ["a", "b"]

Jussi Salmela
Guest
Posts: n/a

 01-07-2007
Michael M. kirjoitti:
> 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>
>
> After, the list ist sorted:
>
> sx1 = ["a", "b", "c", "d"]
> sx2 = ["q", "e", "d"]
> sx3 = ["a", "b"]
>

s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
ss = ((len(s1), s1), (len(s2), s2), (len(s3), s3))
sx = [y for (x, y) in sorted(ss)[::-1]]
print sx
sx1, sx2, sx3 = sx
print sx1, sx2, sx3

Cheers,
Jussi

Felipe Almeida Lessa
Guest
Posts: n/a

 01-07-2007
On 1/7/07, Michael M. <(E-Mail Removed)> wrote:
> How to find the longst element list of lists?

s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]

s = [s1, s2, s3]
s.sort(key=len, reverse=True)
print s[0] is s3
print s[1] is s1
print s[2] is s2

sx1, sx2, sx3 = s
print 'sx1:', sx1
print 'sx2:', sx2
print 'sx3:', sx3

--
Felipe.

Thomas Ploch
Guest
Posts: n/a

 01-07-2007
Michael M. schrieb:
> 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"]
>
> if len(s1) >= len(s2) and len(s1) >= len(s3):
> sx1=s1 ## s1 ist längster
> if len(s2) >= len(s3):
> sx2=s2
> sx3=s3
> else:
> sx2=s3
> sx3=s2
>
> if len(s2) >= len(s3) and len(s2) >= len(s1):
> sx1=s2 ## s2 ist längster
> if len(s3) >= len(s1):
> sx2=s3
> sx3=s1
> else:
> sx2=s1
> sx3=s3
>
> if len(s3) >= len(s1) and len(s3) >= len(s2):
> sx1=s3 ## s3 ist längster
> if len(s1) >= len(s2):
> sx2=s1
> sx3=s2
> else:
> sx2=s2
> sx3=s1
>
> After, the list ist sorted:
>
> sx1 = ["a", "b", "c", "d"]
> sx2 = ["q", "e", "d"]
> sx3 = ["a", "b"]
>

I don't really get that. You have three lists, you want to sort them
after their length. You should put them into one list.
I think you should rather implement this as:

>>> list = [a1, s2, s3]
>>> list.sort(lambda x,y: cmp(len(y), len(x)))
>>> list

[['a', 'b', 'c', 'd'], ['q', 'e', 'd'], ['a', 'b']]

Thomas

Bruno Desthuilliers
Guest
Posts: n/a

 01-07-2007
Michael M. a écrit :
> How to find the longst element list of lists?

For what definition of "find" ? You want the lenght of the longest
sublist, it's index, or a reference to it ?

> I think, there should be an easier way then this:
>
> s1 = ["q", "e", "d"]
> s2 = ["a", "b"]
> s3 = ["a", "b", "c", "d"]

Err... this makes three distinct lists, not a list of lists.

> if len(s1) >= len(s2) and len(s1) >= len(s3):
> sx1=s1 ## s1 ist längster
> if len(s2) >= len(s3):
> sx2=s2
> sx3=s3
> else:
> sx2=s3
> sx3=s2
>

(snip repeated code)

Looks like it would be time to learn how to factor out repetitions...

> After, the list ist sorted:
>
> sx1 = ["a", "b", "c", "d"]
> sx2 = ["q", "e", "d"]
> sx3 = ["a", "b"]
>

This is still not a list of lists. Now for the answer, sorted() is your
friend:

print sorted([s1, s2, s3], key=list.__len__, reverse=True)
=> [['a', 'b', 'c', 'd'], ['q', 'e', 'd'], ['a', 'b']]

# Or if you really want sx1, sx2 and sx3:
sx1, sx2, sx3 = sorted([s1, s2, s3], key=list.__len__, reverse=True)

Is that easier enough ?-)

Michael M.
Guest
Posts: n/a

 01-07-2007
>
> Err... this makes three distinct lists, not a list of lists.
>

Sure. Logically spoken. Not in Python code. Or a number of lists.
Sure not [[ bla... ] [bla.]] etc.

Thomas Ploch
Guest
Posts: n/a

 01-07-2007
Michael M. schrieb:
>> Err... this makes three distinct lists, not a list of lists.
>>

>
> Sure. Logically spoken. Not in Python code. Or a number of lists.
> Sure not [[ bla... ] [bla.]] etc.

???????

Thomas

Michael M.
Guest
Posts: n/a

 01-07-2007
Bruno Desthuilliers wrote:

>
> Err... this makes three distinct lists, not a list of lists.
>

Sure. Logically spoken. Not in Python code. Or a number of lists.
Sure not [[ bla... ] [bla.]] etc.

Michael M.
Guest
Posts: n/a

 01-07-2007
Sorry, wrong place.

Dan Sommers
Guest
Posts: n/a

 01-08-2007
On Sun, 07 Jan 2007 22:23:22 +0100,
"Michael M." <(E-Mail Removed)> 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