-
Notifications
You must be signed in to change notification settings - Fork 286
add sort and sorted in the doc #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
t3chn0tr0n
wants to merge
2
commits into
kushaldas:main
Choose a base branch
from
t3chn0tr0n:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -376,6 +376,108 @@ You may also need to iterate through two sequences same time, for that use *zip( | |
Pradeepto uses OpenSUSE | ||
Kushal uses Fedora | ||
|
||
Sorting using sort() and sorted() | ||
================================= | ||
|
||
Sorting is very essential for any data-structure in any language. We already saw how to use *sort()* in lists. So that solves the sorting of lists, but how to sort other data-structures? Well there is *sorted()*. | ||
|
||
Parameters: `sorted(iterable [, key] [, reverse])`. Where *key* and *reverse* are optional and thus in []s. | ||
|
||
Default sorting order: Ascending. | ||
|
||
Output Format: List (This is important! You need to type cast it back to its original form) | ||
|
||
Sorted() parameters are also applicable for sort() in list objects. BUT *sort()* Does not return anything. Changes are made in the list itself. | ||
|
||
Sorting Sets and Tuples are pretty straight-forward! | ||
|
||
:: | ||
|
||
>>> l = [4, 1, 3, 2] | ||
>>> s = {4, 1, 3, 2} | ||
>>> t = (4, 1, 3, 2) | ||
>>> sorted(l) | ||
[1, 2, 3, 4] | ||
>>> sorted(s) | ||
[1, 2, 3, 4] | ||
>>> sorted(t) | ||
[1, 2, 3, 4] | ||
|
||
Lets see how dictionary can be used: | ||
:: | ||
|
||
>>> d ={ | ||
"a": 1, | ||
"c": 3, | ||
"b": 2 | ||
} | ||
>>> sorted(d) # or sorted(d.keys()) | ||
['a', 'b', 'c'] | ||
>>> sorted(d.values()) | ||
[1, 2, 3] | ||
>>> sorted(d.items()) | ||
[('a', 1), ('b', 3), ('c', 2)] | ||
|
||
The first output: Sorts the dictionary keys, in alphabetical order! | ||
The Second output: Sorts the dictionary values. | ||
The third output: Sorts the dictionary key-value pairs! | ||
|
||
Okay now lets see reverse in action. | ||
|
||
:: | ||
|
||
>>> t = (4, 1, 3, 2) | ||
>>> sorted(t, reverse=True) | ||
[4, 3, 2, 1] | ||
|
||
That was pretty simple. Using *key* can be very crucial. It serves as a key for the sort comparison. Most times lambda functions are used in the key field. We well see one simple and one with lambda function in the examples. | ||
|
||
:: | ||
|
||
>>> # Example: Sorting a list of Strings, using the length of tuples as the key. | ||
>>> l = ['ab', 'abcd', 'a', 'abc'] | ||
>>> sorted(l, key=len) | ||
['a', 'ab', 'abc', 'abcd'] | ||
|
||
Here the len() us operated on each String objects. Thus any function can be used, even user defined ones, which takes one parameter and returns a value. Example: | ||
|
||
:: | ||
|
||
>>> # Sorting a list of tuples using the 2nd element of the tuple | ||
>>> def return_2nd(elem): | ||
... return elem[1] | ||
|
||
>>> l1 = [(1,2), (2,1), (3,2), (1,5)] | ||
>>> l2 = [(1,2), (2,1), (3,2), (1,5)] | ||
>>> l1.sort(key=return_2nd) | ||
>>> print(l1) | ||
[(2, 1), (1, 2), (3, 2), (1, 5)] | ||
>>> l2.sort(key=lambda x: x[1]) | ||
>>> print(l2) | ||
[(2, 1), (1, 2), (3, 2), (1, 5)] # same output using lambda | ||
|
||
Two things to note here: | ||
|
||
1. See how I used sort(), although sorted could have done the same thing. Note: Had to use print() as sort does not return anything. | ||
2. See the use of lambda function. It replaces the need to write the definition and works in 1 line. | ||
But keep in mind, complicated lambda functions makes your code unreadable. | ||
|
||
Okay now one last thing about sorts, this is an awesome feature - what if in the above example I want this: "If the 2nd tuple elements are equal, check for the 1st elements", then what? | ||
|
||
Well *key* can take a list for such cases to chain sorting. Format: [option1, option2, ...] where, when option1 is equal, it checks for option2 between them. Example: | ||
|
||
:: | ||
|
||
>>> l = [(3, 2), (3, 4), (1, 2), (2, 4)] | ||
>>> sorted(l, key=lambda x: x[1]) # Sorting by just 2nd element | ||
[(3, 2), (1, 2), (3, 4), (2, 4)] | ||
>>> sorted(l, key=lambda x: [x[1], x[0]]) # Sorting by 2nd element, if equal, check 1st | ||
[(1, 2), (3, 2), (2, 4), (3, 4)] | ||
|
||
Whenever two 2nd elements were equal, 1st element was checked. eg. in (3, 2), (1, 2), since 2s were equal, 1 and 3 was checked. and (1,2) came up in-front. | ||
|
||
Now lets see an example. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example of ? |
||
students.py | ||
=========== | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.