Sorting in Python

Python has a lot of applications, from chatbots to API’s, we have a module for almost everything. But in order to get the most out of it, we should know the basics. One of the basic things in a language that is used almost every now and then is sorting. Here’s how to use sorting in Python using the prebuilt methods.

Options

  1. sorted()
  2. .sort()

sorted()

The method that even the Python docs suggest is sorted. Under the hood, Python uses timsort.

Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.
[Here’s] a better explanation.

The method takes just a few arguments.

sorted(iterable, *, key=None, reverse=False)
sorted(iterable, *, key=None, reverse=False)
sorted(iterable, *, key=None, reverse=False)

Enter fullscreen mode Exit fullscreen mode

It’s pretty straightforward to understand the arguments.

iterable

Iterable is any datatype that can be iterated or looped over. Python currently supports tuple, list, or dictionaries.

<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>,</span> <span>4</span><span>,</span> <span>5</span><span>]</span>
<span># Sort using the sorted method </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>)</span>
<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>,</span> <span>4</span><span>,</span> <span>5</span><span>]</span>

<span># Sort using the sorted method </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>)</span>
d = [1, 2, 3, 4, 5] # Sort using the sorted method d = sorted(d)

Enter fullscreen mode Exit fullscreen mode

key

Key is a function that takes one argument. Each element that is got while iterating over the iterable is passed as an argument to this function.

The return value of this function is used to compare where the passed arguments position should be.

The default value is None.

<span>d</span> <span>=</span> <span>[</span>
<span>[</span><span>2</span><span>,</span> <span>"second"</span><span>],</span>
<span>[</span><span>3</span><span>,</span> <span>"third"</span><span>],</span>
<span>[</span><span>-</span><span>1</span><span>,</span> <span>"first"</span><span>]</span>
<span>]</span>
<span># Function to return the zeroth element in each # object </span><span>def</span> <span>getzeroth</span><span>(</span><span>el</span><span>):</span>
<span>return</span> <span>el</span><span>[</span><span>0</span><span>]</span>
<span># Sort the dictionary by using the 0th # element in each object. </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>key</span><span>=</span><span>getzeroth</span><span>)</span>
<span>d</span> <span>=</span> <span>[</span>
  <span>[</span><span>2</span><span>,</span> <span>"second"</span><span>],</span>
  <span>[</span><span>3</span><span>,</span> <span>"third"</span><span>],</span>
  <span>[</span><span>-</span><span>1</span><span>,</span> <span>"first"</span><span>]</span>
<span>]</span>

<span># Function to return the zeroth element in each # object </span><span>def</span> <span>getzeroth</span><span>(</span><span>el</span><span>):</span>
    <span>return</span> <span>el</span><span>[</span><span>0</span><span>]</span>

<span># Sort the dictionary by using the 0th # element in each object. </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>key</span><span>=</span><span>getzeroth</span><span>)</span>
d = [ [2, "second"], [3, "third"], [-1, "first"] ] # Function to return the zeroth element in each # object def getzeroth(el): return el[0] # Sort the dictionary by using the 0th # element in each object. d = sorted(d, key=getzeroth)

Enter fullscreen mode Exit fullscreen mode

NOTE: We can also use a lambda function instead of defining a whole function.

<span># Sort using the zeroth element using a lambda # function </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>key</span><span>=</span><span>lambda</span> <span>x</span><span>:</span> <span>x</span><span>[</span><span>0</span><span>])</span>
<span># Sort using the zeroth element using a lambda # function </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>key</span><span>=</span><span>lambda</span> <span>x</span><span>:</span> <span>x</span><span>[</span><span>0</span><span>])</span>
# Sort using the zeroth element using a lambda # function d = sorted(d, key=lambda x: x[0])

Enter fullscreen mode Exit fullscreen mode

reverse

This argument just takes a bool as argument and the default value is False. If True is passed then the result will be reversed, i:e the iterable will be sorted in descending order.

<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>]</span>
<span># Make the list reverse ordered </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>reverse</span><span>=</span><span>True</span><span>)</span>
<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>]</span>

<span># Make the list reverse ordered </span><span>d</span> <span>=</span> <span>sorted</span><span>(</span><span>d</span><span>,</span> <span>reverse</span><span>=</span><span>True</span><span>)</span>
d = [1, 2, 3] # Make the list reverse ordered d = sorted(d, reverse=True)

Enter fullscreen mode Exit fullscreen mode

.sort()

The sort method sorts the list in place. It is a method of the list class itself so it takes one argument less than sorted. Just like sorted this method uses Timsort under the hood.

This method is usually simpler to use with lists.

The arguments passed are:

sort(*, key=None, reverse=False)
sort(*, key=None, reverse=False)
sort(*, key=None, reverse=False)

Enter fullscreen mode Exit fullscreen mode

Just like sorted we can pass key and reverse in this method as well.

<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>5</span><span>,</span><span>27</span><span>,</span> <span>89</span><span>,</span> <span>-</span><span>90</span><span>]</span>
<span># Sort the list using .sort </span><span>d</span><span>.</span><span>sort</span><span>()</span>
<span>d</span> <span>=</span> <span>[</span><span>1</span><span>,</span> <span>5</span><span>,</span><span>27</span><span>,</span> <span>89</span><span>,</span> <span>-</span><span>90</span><span>]</span>

<span># Sort the list using .sort </span><span>d</span><span>.</span><span>sort</span><span>()</span>
d = [1, 5,27, 89, -90] # Sort the list using .sort d.sort()

Enter fullscreen mode Exit fullscreen mode

This article was aimed at those who are new to Python or even advanced Python programmers but need some kind of a bookmarkable post to look at the sorting methods in Python.

I also write at my personal site https://deepjyoti30.github.io/ about Python, technology and other stuff. Do check it out.

原文链接:Sorting in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Flat rich prosperous time in vain to develop a group of coward, hardship is the mother of strong forever.
平富足的盛世徒然养成一批懦夫,困苦永远是坚强之母
评论 抢沙发

请登录后发表评论

    暂无评论内容