Actual question:
I have a string s and I need to sort it using below 3 criteria. I already have the solution but I need to understand a part of that solution. How is it working?
- All sorted lowercase letters are ahead of uppercase letters.
- All sorted uppercase letters are ahead of digits.
- All sorted odd digits are ahead of sorted even digits.
s="Sa27"
print(*sorted(s, key = lambda x: ( x.isdigit() and int(x)%2==0
, x.isdigit(),x.isupper(),x.islower(),x)), sep = '')
I am getting the expected out from the above code: aS72
The stuff inside the lambda gives tuple of True, false values shown below. I want to know how these tuples are actually determining the order/priority of the element.
(False, False, True, False, 'S')
(False, False, False, True, 'a')
(True, True, False, False, '2')
(False, True, False, False, '7')
False < Trueholds.