We have summarized the ** sorting ** methods that are important for working with lists. Basically, write the code and output result with the following code.
ex.py
code = 'code'
print(code)
# code(Output result)
There are two ways to sort a list in ascending or descending order in Python: sort ()
and sorted ()
. Use sorted ()
if you want to sort strings and tuples.
** * Destructive processing that rewrites the original list itself. ** **
sort().py
org_list = [3, 1, 4, 5, 2]
org_list.sort()
print(org_list)
# [1, 2, 3, 4, 5]
#sort()Note that returns None.
print(org_list.sort())
# None
#The default is ascending order. If you want to sort in descending order, set the argument reverse to True.
org_list.sort(reverse=True)
print(org_list)
# [5, 4, 3, 2, 1]
** * If you specify the list you want to sort in the argument, the sorted list is returned. Non-destructive processing that does not change the original list. ** **
sorted().py
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
# [3, 1, 4, 5, 2]
print(new_list)
# [1, 2, 3, 4, 5]
#The default is ascending order. If you want to sort in descending order, set the argument reverse to True.
new_list_reverse = sorted(org_list, reverse=True)
print(org_list)
# [3, 1, 4, 5, 2]
print(new_list_reverse)
# [5, 4, 3, 2, 1]
If a character string is specified as an argument of the sorted () function, a ** list ** in which each character of the sorted character string is stored as an element is returned.
sorted()_.py
org_str = 'cebad'
print(org_str)
# cebad
new_str_list = sorted(org_str)
print(new_str_list)
# ['a', 'b', 'c', 'd', 'e']
#Join to concatenate a list of strings into a single string()Use the method.
new_str = ''.join(new_str_list)
print(new_str)
# abcde
#You can write them all together. If you want to sort in descending order, set the argument reverse to True.
new_str_reverse = ''.join(sorted(org_str, reverse=True))
print(new_str_reverse)
# edcba
There are ways to sort the elements of a list in reverse order in Python using reverse ()
, reversed ()
and slices. Use reversed ()
or slices if you want to sort strings or tuples in reverse order.
** * Destructive processing that rewrites the original list itself. ** **
sort().py
org_list = [1, 2, 3, 4, 5]
org_list.reverse()
print(org_list)
# [5, 4, 3, 2, 1]
#reverse()Note that returns None.
print(org_list.reverse())
# None
** * reversed () returns an iterator that retrieves the elements in reverse order. Non-destructive processing that does not change the original list. ** **
sort().py
org_list = [1, 2, 3, 4, 5]
reverse_iterator = reversed(org_list)
print(org_list)
# [1, 2, 3, 4, 5]
#reversed()Note that returns an iterator instead of a list. list()Convert the iterator to a list with.
new_list = list(reversed(org_list))
print(new_list)
# [5, 4, 3, 2, 1]
ex.py
org_list = [1, 2, 3, 4, 5]
new_list = org_list[::-1]
print(new_list)
# [5, 4, 3, 2, 1]
org_str = 'abcde'
new_str = org_str[::-1]
print(new_str)
# edcba
I would be grateful if you could tell me if there is any other solution.
Recommended Posts