I run a following code from the Python textbook:
strings = ['foo','card','bar','aaaa','abab']
strings.sort(key= lambda x : len(set(list(x))))
print(strings)
Out: ['aaaa', 'foo', 'abab', 'bar', 'card']
Why is the list sorted in this order? It is neither due to number of letters, alphabetical order, nor ASCII codes.
In the book the explanation says that words are sorted taking into account number of letters in each word; yet this should place them in order from shortest to longest, which is not the case.
1
This is sorting based on the number of unique letters in each string:
# number of unique letters
# 1 2 2 3 4
['aaaa', 'foo', 'abab', 'bar', 'card']
Here is a breakdown of the steps:
- get letters (
list
) (this step is not needed,set(x)
is enough) - find unique ones (
set
) - count them (
len
)
# strings
['foo','card','bar','aaaa','abab']
# sets: [set(x) for x in strings]
[{'f', 'o'}, {'a', 'c', 'd', 'r'}, {'a', 'b', 'r'}, {'a'}, {'a', 'b'}]
# lengths: [len(set(x)) for x in strings]
[2, 4, 3, 1, 2]
2
len(set(list(x)))
is giving you the length of unique characters in list(x)
.
So “aaaa” has 1, “foo” and “abab” have 2, etc.