Sorting List By Alphabetical Order, With Special Case For Certain Starting Letter
Solution 1:
sorted
or list.sort
accepts optional key
keyword argument. That is the function used to get sort key, and the return value of the function is used to compare instead of the original items.
>>> words = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
>>> sorted(words, key=lambda word: (word[0] != 'x', word))
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
used word[0] != 'x'
; which return False
(0) for word starts with x
, True
(1) for other words; resulting words start with x
come first.
Solution 2:
words = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
result = [i for _, i in sorted((word[0]!='x', word) for word in words)]
Solution 3:
Just make a particular case in your key method: if starts with x, return a truncated string starting with "0" so it will appear first, but will be still sorted after "x".
z=['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
z.sort(key=lambda x : "0"+x if x.startswith("x") else x)
print(z)
yields:
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
Solution 4:
>>> sorted(['mix', 'xyz', 'apple', 'xanadu', 'aardvark'],
key=lambda x: (not x.startswith('x'), x))
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
The sorted()
builtin returns a new, stable-sorted list()
of each element in the input iterable, sorted by key
. key
, in this case, is a lambda expression (basically a "mini-function") which transforms or converts each element in the input list into a sortable value.
In this case, our lambda expression sorts each word in the list by a tuple()
which contains False
or True
indicating whether or not the word starts with "x"
, followed by the word itself. Since False
is "smaller" than True
, the words that start with "x"
appear first, and everything else is sorted alphabetically.
Solution 5:
You can sort it using the built-in list.sort()
and then use list comprehension to get the output you desire as follows:
sl = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
sl.sort()
sl = [el forelin sl if el.startswith("x")]+[el forelin sl if not el.startswith("x")]
Post a Comment for "Sorting List By Alphabetical Order, With Special Case For Certain Starting Letter"