From ed8394afc851e6c82fa31b1cbb03a520dd5ab1aa Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Sat, 4 Jan 2020 14:00:20 +0100 Subject: [PATCH 1/5] Suggests using builtin functions for median, sample, shuffle, split_lines and zip --- snippets/median.md | 2 ++ snippets/sample.md | 2 ++ snippets/shuffle.md | 2 ++ snippets/split_lines.md | 2 ++ snippets/zip.md | 2 ++ 5 files changed, 10 insertions(+) diff --git a/snippets/median.md b/snippets/median.md index ddc003a06..73665ff4c 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -7,6 +7,8 @@ Finds the median of a list of numbers. Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even. +[`median`](https://docs.python.org/3/library/statistics.html#statistics.median) provides similar functionality to this snippet. + ```py def median(list): list.sort() diff --git a/snippets/sample.md b/snippets/sample.md index f280e61be..417eb672a 100644 --- a/snippets/sample.md +++ b/snippets/sample.md @@ -7,6 +7,8 @@ Returns a random element from a list. Use `random.randint()` to generate a random number that corresponds to an index in the list, return the element at that index. +[`random.sample`](https://docs.python.org/3/library/random.html#random.sample) provides similar functionality to this snippet. + ```py from random import randint diff --git a/snippets/shuffle.md b/snippets/shuffle.md index abbd2b3b6..d5ca0ba78 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -7,6 +7,8 @@ Randomizes the order of the values of an list, returning a new list. Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list. +[`random.shuffle`](https://docs.python.org/3/library/random.html#random.shuffle) provides similar functionality to this snippet. + ```py from copy import deepcopy from random import randint diff --git a/snippets/split_lines.md b/snippets/split_lines.md index 80fe9c276..0b1d1ecef 100644 --- a/snippets/split_lines.md +++ b/snippets/split_lines.md @@ -7,6 +7,8 @@ Splits a multiline string into a list of lines. Use `s.split()` and `'\n'` to match line breaks and create a list. +[`str.splitlines`](https://docs.python.org/3/library/stdtypes.html#str.splitlines) provides similar functionality to this snippet. + ```py def split_lines(s): return s.split('\n') diff --git a/snippets/zip.md b/snippets/zip.md index a9fb819d2..b16696763 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -9,6 +9,8 @@ Use `max` combined with `list comprehension` to get the length of the longest li Loop for `max_length` times grouping elements. If lengths of `lists` vary, use `fill_value` (defaults to `None`). +[`zip`](https://docs.python.org/3/library/functions.html#zip) and [`itertools.zip_longest`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) provide similar functionality to this snippet. + ```py def zip(*args, fill_value=None): max_length = max([len(lst) for lst in args]) From 84ddb26382558b37530888835b336664cf6d7ccd Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:24:33 +0200 Subject: [PATCH 2/5] Update median.md --- snippets/median.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/median.md b/snippets/median.md index 73665ff4c..2cfcb91bc 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -7,7 +7,7 @@ Finds the median of a list of numbers. Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even. -[`median`](https://docs.python.org/3/library/statistics.html#statistics.median) provides similar functionality to this snippet. +[`statistics.median()`](https://docs.python.org/3/library/statistics.html#statistics.median) provides similar functionality to this snippet. ```py def median(list): From 1648eaafad0dac018943786c90d5bf134d6f3ea4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:25:14 +0200 Subject: [PATCH 3/5] Update sample.md --- snippets/sample.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sample.md b/snippets/sample.md index 417eb672a..18eb6b786 100644 --- a/snippets/sample.md +++ b/snippets/sample.md @@ -7,7 +7,7 @@ Returns a random element from a list. Use `random.randint()` to generate a random number that corresponds to an index in the list, return the element at that index. -[`random.sample`](https://docs.python.org/3/library/random.html#random.sample) provides similar functionality to this snippet. +[`random.sample()`](https://docs.python.org/3/library/random.html#random.sample) provides similar functionality to this snippet. ```py from random import randint From a8f2284db324701df77726a93f4adf9741dd605d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:25:31 +0200 Subject: [PATCH 4/5] Update split_lines.md --- snippets/split_lines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/split_lines.md b/snippets/split_lines.md index 0b1d1ecef..fdafdb0a0 100644 --- a/snippets/split_lines.md +++ b/snippets/split_lines.md @@ -7,7 +7,7 @@ Splits a multiline string into a list of lines. Use `s.split()` and `'\n'` to match line breaks and create a list. -[`str.splitlines`](https://docs.python.org/3/library/stdtypes.html#str.splitlines) provides similar functionality to this snippet. +[`str.splitlines()`](https://docs.python.org/3/library/stdtypes.html#str.splitlines) provides similar functionality to this snippet. ```py def split_lines(s): From caad1431caba8eb9697757d543f5733dfc22f167 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:25:58 +0200 Subject: [PATCH 5/5] Update zip.md --- snippets/zip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/zip.md b/snippets/zip.md index b16696763..88d02c385 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -9,7 +9,7 @@ Use `max` combined with `list comprehension` to get the length of the longest li Loop for `max_length` times grouping elements. If lengths of `lists` vary, use `fill_value` (defaults to `None`). -[`zip`](https://docs.python.org/3/library/functions.html#zip) and [`itertools.zip_longest`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) provide similar functionality to this snippet. +[`zip()`](https://docs.python.org/3/library/functions.html#zip) and [`itertools.zip_longest()`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) provide similar functionality to this snippet. ```py def zip(*args, fill_value=None):