words = "4of Fo1r pe6ople g3ood th5e the2"
words = sorted(words.split(), key=lambda w:sorted(w))
output:
['Fo1r', 'the2', 'g3ood', '4of', 'th5e', 'pe6ople']
I don't get how this function sorts the words based on the number in the word
words = "4of Fo1r pe6ople g3ood th5e the2"
words = sorted(words.split(), key=lambda w:sorted(w))
output:
['Fo1r', 'the2', 'g3ood', '4of', 'th5e', 'pe6ople']
I don't get how this function sorts the words based on the number in the word
The inner sorted
orders symbols (letters and numbers) from each word according to their numeric code, which is more or less alphabetical, with numbers preceding the letters. As you might noted, Python strings are often handled in the same way as any other iterables, such as lists, sets or dictionaries. For instance, sorted("of4")
results in the list ["4", "f", "o"]
because the number symbols are considered to go before letters. sorted
maps ("For1"
) into a list starting with "1". This list precedes the one starting with "4"
, and the rest of lists.
Apply sorted
to each of words, and it will be more clear to you.
More technically, sorted
transforms a word into an ordered list of 1-letter string. The outer sorted
orders those words by comparing corresponding ordered lists. The Python lists are compared element by element.
sorted
's key
argument is a function called on each element of the input to produce the value to sort on. So when the key
itself is also sorted
(note: key=lambda w:sorted(w)
is just a slow way to spell key=sorted
), it means it sorts 'Fo1r'
to produce a key
value of ['1', 'F', 'o', 'r']
. Since sorting characters effectively sorts them by ordinal value, and ASCII digits precede ASCII letters on ordinal value, it means that in this particular case, with one unique digit in each input, and the rest of each string being letters, it effectively sorts by the digit.
If the same digit appeared in more than one input, it would fallback to sorting by the highest ordinal value aside from the digit; e.g. 'abc1'
would sort before 'xyz1'
because the fallback comparison would be comparing 'a'
to 'x'
. Similarly, if a space character appeared in some inputs, but not others, those inputs would sort before all the others (because the space character is ordinal 32, while '0'
is ordinal 48).
Here are your words:
>>> words = "4of Fo1r pe6ople g3ood th5e the2"
>>> words = words.split()
>>> words
['4of', 'Fo1r', 'pe6ople', 'g3ood', 'th5e', 'the2']
You can sort the letters of each word:
>>> [sorted(w) for w in words]
[['4', 'f', 'o'], ['1', 'F', 'o', 'r'], ['6', 'e', 'e', 'l', 'o', 'p', 'p'], ['3', 'd', 'g', 'o', 'o'], ['5', 'e', 'h', 't'], ['2', 'e', 'h', 't']]
If you zip
words and the previous list, you see each word along with the key:
>>> list(zip(words, [sorted(w) for w in words]))
[('4of', ['4', 'f', 'o']), ('Fo1r', ['1', 'F', 'o', 'r']), ('pe6ople', ['6', 'e', 'e', 'l', 'o', 'p', 'p']), ('g3ood', ['3', 'd', 'g', 'o', 'o']), ('th5e', ['5', 'e', 'h', 't']), ('the2', ['2', 'e', 'h', 't'])]
That's why Fo1r
(key: ['1', 'F', 'o', 'r']
) is before the2
(key: ['2', 'e', 'h', 't']
) and so on...
instead of "sorted" function you can use your version of function which compare string based on digits present in the string..
below i have shown function for the same
>>> def mysort(string):
lst=[]
final=''
for char in string:
if char.isdigit():
lst.append(char)
lst.sort()
for i in lst:
for word in string.split():
if i in word:
final=final+' '+word
return final
output:
>>> words= "4of Fo1r pe6ople g3ood th5e the2"
>>> mysort(words)
' Fo1r the2 g3ood 4of th5e pe6ople'