From 48fc7ebf2935c12b8764d47fe1a42db60d248b22 Mon Sep 17 00:00:00 2001 From: Sahith Kurapati <52966918+Sahith02@users.noreply.github.com> Date: Fri, 19 Jun 2020 17:14:35 +0530 Subject: [PATCH 1/3] Added new snippet Added sort_list_based_on_another_list.md snippet. It sorts one list based on another list in python. --- snippets/sort_list_based_on_another_list.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/sort_list_based_on_another_list.md diff --git a/snippets/sort_list_based_on_another_list.md b/snippets/sort_list_based_on_another_list.md new file mode 100644 index 000000000..137f26b17 --- /dev/null +++ b/snippets/sort_list_based_on_another_list.md @@ -0,0 +1,19 @@ +--- +title: sort_list_based_on_another_list +tags: list,sort,intermediate +--- + +Sorts one list based on given indices in another list and returns the sorted list of values. + +Use list comprehension to get values after using `zip` and `sorted` function based on given `key` with lambda function that returns the index. + +```py +def sort_list_based_on_another_list(to_sort, index_list): + return [val for i, val in sorted(zip(index_list, to_sort), key = lambda x: x[0])] +``` + +```py +a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] +b = [3, 2, 6, 4, 1, 5] +sort_list_based_on_another_list(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges'] +``` \ No newline at end of file From a1357e543d54bc953feea92ef219ecb4d963a704 Mon Sep 17 00:00:00 2001 From: Sahith Kurapati <52966918+Sahith02@users.noreply.github.com> Date: Fri, 19 Jun 2020 17:27:35 +0530 Subject: [PATCH 2/3] Changed hard tabs to soft tabs(2 spaces) --- snippets/sort_list_based_on_another_list.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/sort_list_based_on_another_list.md b/snippets/sort_list_based_on_another_list.md index 137f26b17..77b2e9e0e 100644 --- a/snippets/sort_list_based_on_another_list.md +++ b/snippets/sort_list_based_on_another_list.md @@ -8,12 +8,12 @@ Sorts one list based on given indices in another list and returns the sorted lis Use list comprehension to get values after using `zip` and `sorted` function based on given `key` with lambda function that returns the index. ```py -def sort_list_based_on_another_list(to_sort, index_list): - return [val for i, val in sorted(zip(index_list, to_sort), key = lambda x: x[0])] +def sort_list_based_on_another_list(list_to_sort, index_list): + return [val for i, val in sorted(zip(index_list, list_to_sort), key = lambda x: x[0])] ``` ```py a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] b = [3, 2, 6, 4, 1, 5] sort_list_based_on_another_list(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges'] -``` \ No newline at end of file +``` From 6f8dd5e9411585e69f7c3c3d07227da2b6365932 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 7 Sep 2020 09:58:39 +0300 Subject: [PATCH 3/3] Update and rename sort_list_based_on_another_list.md to sort_by_indexes.md --- snippets/sort_by_indexes.md | 20 ++++++++++++++++++++ snippets/sort_list_based_on_another_list.md | 19 ------------------- 2 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 snippets/sort_by_indexes.md delete mode 100644 snippets/sort_list_based_on_another_list.md diff --git a/snippets/sort_by_indexes.md b/snippets/sort_by_indexes.md new file mode 100644 index 000000000..38539308b --- /dev/null +++ b/snippets/sort_by_indexes.md @@ -0,0 +1,20 @@ +--- +title: sort_by_indexes +tags: list,sort,intermediate +--- + +Sorts one list based on another list containing the desired indexes. + +Use `zip()` and `sorted()` to combine and sort the two lists, based on the values of `indexes`. +Use a list comprehension to get the first element of each pair from the result. + +```py +def sort_by_indexes(lst, indexes): + return [val for _, val in sorted(zip(indexes, lst), key = lambda x: x[0])] +``` + +```py +a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] +b = [3, 2, 6, 4, 1, 5] +sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges'] +``` diff --git a/snippets/sort_list_based_on_another_list.md b/snippets/sort_list_based_on_another_list.md deleted file mode 100644 index 77b2e9e0e..000000000 --- a/snippets/sort_list_based_on_another_list.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: sort_list_based_on_another_list -tags: list,sort,intermediate ---- - -Sorts one list based on given indices in another list and returns the sorted list of values. - -Use list comprehension to get values after using `zip` and `sorted` function based on given `key` with lambda function that returns the index. - -```py -def sort_list_based_on_another_list(list_to_sort, index_list): - return [val for i, val in sorted(zip(index_list, list_to_sort), key = lambda x: x[0])] -``` - -```py -a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] -b = [3, 2, 6, 4, 1, 5] -sort_list_based_on_another_list(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges'] -```