From 84ad5f87fcd6aefa3d5a36ed9d9558fbcb44e1d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ad=C3=A1n=20Hern=C3=A1ndez=20Baena?= <39740586+HBaena@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:03:20 -0500 Subject: [PATCH 1/4] Transpose matrix --- snippets/transpose.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 snippets/transpose.md diff --git a/snippets/transpose.md b/snippets/transpose.md new file mode 100644 index 000000000..6ec255e5b --- /dev/null +++ b/snippets/transpose.md @@ -0,0 +1,10 @@ +## Make a transpose matrix + +```python +def transpose(l): + return list(zip(*l)) + +l = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] +print(transpose(l)) +# Output: [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] +``` From db7c1358b344f0e92e28971896f174d18de19435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ad=C3=A1n=20Hern=C3=A1ndez=20Baena?= <39740586+HBaena@users.noreply.github.com> Date: Wed, 2 Oct 2019 23:53:22 -0500 Subject: [PATCH 2/4] Correcting the snippet format Adding title, tags, explanation. --- snippets/transpose.md | 10 ---------- snippets/transpose_matrix.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) delete mode 100644 snippets/transpose.md create mode 100644 snippets/transpose_matrix.md diff --git a/snippets/transpose.md b/snippets/transpose.md deleted file mode 100644 index 6ec255e5b..000000000 --- a/snippets/transpose.md +++ /dev/null @@ -1,10 +0,0 @@ -## Make a transpose matrix - -```python -def transpose(l): - return list(zip(*l)) - -l = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] -print(transpose(l)) -# Output: [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] -``` diff --git a/snippets/transpose_matrix.md b/snippets/transpose_matrix.md new file mode 100644 index 000000000..a99aa99d4 --- /dev/null +++ b/snippets/transpose_matrix.md @@ -0,0 +1,29 @@ +--- +title: transpose_matrix +tags: matrix,list,zip,intermediate +--- + +Python `zip()` function takes the iterable elements as input and returns the iterator. +Since zip funtion returns a `zip object`, we can cast it as list wit `list()`. +```py +list(zip([1,2,3],['a','b','c'], [True, False, True])) +# Output: [(1, 'a', True), (2, 'b', False), (3, 'c', True)] +``` + +Using `*list`, we can access to each element of the list +```py +[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] +# Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] +*[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] +# Output: [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] +``` + +Combining these two elements we can transpose a matrix. We can access to each row of the matrix using the operator `*` to use them as args of `zip()` function. + +```py +def transpose_matrix(l:list): + return list(zip(*l)) + +print(transpose_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])) +# Output: [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] +``` From 81d2d7f0b03f09ce270a284c92c405deef05a56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ad=C3=A1n=20Hern=C3=A1ndez=20Baena?= <39740586+HBaena@users.noreply.github.com> Date: Thu, 3 Oct 2019 09:02:20 -0500 Subject: [PATCH 3/4] Correcting snippet template Correcting snippet template for transpose_matrix function. Adding briefly explanation and modifying the code. --- snippets/transpose_matrix.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/snippets/transpose_matrix.md b/snippets/transpose_matrix.md index a99aa99d4..4264776cd 100644 --- a/snippets/transpose_matrix.md +++ b/snippets/transpose_matrix.md @@ -3,19 +3,18 @@ title: transpose_matrix tags: matrix,list,zip,intermediate --- +Obtaining the transpose of a matrix making use of `zip()` function. + Python `zip()` function takes the iterable elements as input and returns the iterator. Since zip funtion returns a `zip object`, we can cast it as list wit `list()`. ```py -list(zip([1,2,3],['a','b','c'], [True, False, True])) -# Output: [(1, 'a', True), (2, 'b', False), (3, 'c', True)] +list(zip([1,2,3],['a','b','c'], [True, False, True])) # [(1, 'a', True), (2, 'b', False), (3, 'c', True)] ``` Using `*list`, we can access to each element of the list ```py -[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] -# Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] -*[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] -# Output: [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] +[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] +*[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] ``` Combining these two elements we can transpose a matrix. We can access to each row of the matrix using the operator `*` to use them as args of `zip()` function. @@ -23,7 +22,8 @@ Combining these two elements we can transpose a matrix. We can access to each ro ```py def transpose_matrix(l:list): return list(zip(*l)) - -print(transpose_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])) -# Output: [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] +``` + +```py +print(transpose_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])) # [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] ``` From 6f7e8edf817508028bfd558b1e43a8d23615093a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 8 Oct 2019 09:53:08 +0300 Subject: [PATCH 4/4] Update and rename transpose_matrix.md to transpose.md --- snippets/transpose.md | 18 ++++++++++++++++++ snippets/transpose_matrix.md | 29 ----------------------------- 2 files changed, 18 insertions(+), 29 deletions(-) create mode 100644 snippets/transpose.md delete mode 100644 snippets/transpose_matrix.md diff --git a/snippets/transpose.md b/snippets/transpose.md new file mode 100644 index 000000000..40f90b096 --- /dev/null +++ b/snippets/transpose.md @@ -0,0 +1,18 @@ +--- +title: transpose +tags: list,intermediate +--- + +Returns the transpose of a two-dimensional list. + +Use `*lst` to get the passed list as tuples. +Use `zip()` in combination with `list()` to create the transpose of the given two-dimensional list. + +```py +def transpose(lst): + return list(zip(*lst)) +``` + +```py +transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] +``` diff --git a/snippets/transpose_matrix.md b/snippets/transpose_matrix.md deleted file mode 100644 index 4264776cd..000000000 --- a/snippets/transpose_matrix.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -title: transpose_matrix -tags: matrix,list,zip,intermediate ---- - -Obtaining the transpose of a matrix making use of `zip()` function. - -Python `zip()` function takes the iterable elements as input and returns the iterator. -Since zip funtion returns a `zip object`, we can cast it as list wit `list()`. -```py -list(zip([1,2,3],['a','b','c'], [True, False, True])) # [(1, 'a', True), (2, 'b', False), (3, 'c', True)] -``` - -Using `*list`, we can access to each element of the list -```py -[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] -*[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12] -``` - -Combining these two elements we can transpose a matrix. We can access to each row of the matrix using the operator `*` to use them as args of `zip()` function. - -```py -def transpose_matrix(l:list): - return list(zip(*l)) -``` - -```py -print(transpose_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])) # [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] -```