gali prem sagar
gali prem sagar

Reputation: 23

Sorting a group of words inside a list

I'm aware with sorting a list of list containing words by length. Which means a list :

[[],['hello','indent'],['hi','monday'],['hi','low']]

which result if sorting key is length and reverse is True:

[['hello','indent','joe'],['hi','monday'],['hi','low'],[]]

But what i want is sort both by length and the ones having same length must be sorted in alphabetical order. i.e., 'low'<'monday' so output should be:

[['hello','indent','joe'],['hi','low'],['hi','monday'],[]]

Which kind of key must i use to sort using inbuilt sort?

EDIT: but what if the inputs are mixed case? What if it is:

[['Hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]

and the desired output will be :

[['hello', 'indent', 'joe'], ['hi', 'monday'],['Hi', 'low'], []]

Upvotes: 2

Views: 281

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55479

This can be done in a single pass with a suitable key function.

a = [['hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
a.sort(key=lambda l:(-len(l), l))
print a

output

[['hello', 'indent', 'joe'], ['hi', 'low'], ['hi', 'monday'], []]

To get lowercase letters to precede uppercase letters we can simply use the str.swapcase() method on the strings in each sub-list:

a = [['Hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
a.sort(key=lambda l:(-len(l), [s.swapcase() for s in l]))
print a

output

[['hello', 'indent', 'joe'], ['hi', 'low'], ['Hi', 'monday'], []]

And if you want the sorting to be case-insensitive:

a = [['Hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
a.sort(key=lambda l:(-len(l), [s.lower() for s in l]))
print a

output

[['hello', 'indent', 'joe'], ['hi', 'low'], ['Hi', 'monday'], []]

Upvotes: 1

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

First sort by alphabetical order, then sort by length in reverse order.

>>> lst = [['hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
>>> lst.sort()
>>> lst.sort(key=len, reverse=True)
>>> print lst
>>> [['hello', 'indent', 'joe'], ['hi', 'low'], ['hi', 'monday'], []]

The order of items in the resulting set highly depends on your current locale. If you want your sort algorithm to take locale into account when sorting items, you can do the following;

>>> import locale
>>> from functools import cmp_to_key
>>>
>>> # You can change your locale like below;
>>> # locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
>>>
>>> lst = [['hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
>>> print sorted([sorted(item, key=cmp_to_key(locale.strcoll)) for item in lst], key=len, reverse=True)
>>> [['hello', 'indent', 'joe'], ['hi', 'monday'], ['hi', 'low'], []]

Upvotes: 1

Related Questions