From d0f6c92df65d551b12cc914f15bd20994729c7ce Mon Sep 17 00:00:00 2001 From: SavagePastaMan <69145546+SavagePastaMan@users.noreply.github.com> Date: Mon, 12 Apr 2021 11:20:43 -0400 Subject: Create str-join.md Create a tag to showcase `str.join` --- bot/resources/tags/str-join.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 bot/resources/tags/str-join.md diff --git a/bot/resources/tags/str-join.md b/bot/resources/tags/str-join.md new file mode 100644 index 000000000..e8407ac26 --- /dev/null +++ b/bot/resources/tags/str-join.md @@ -0,0 +1,25 @@ +**Joining Iterables** + +Suppose you want to nicely display a list (or some other iterable). The naive solution would be something like this. +```py +colors = ['red', 'green', 'blue', 'yellow'] +output = "" +separator = ", " +for color in colors: + output += color + separator +print(output) # Prints 'red, green, blue, yellow, ' +``` +However, this solution is flawed. The separator is still added to the last color, and it is slow. + +The better way is to use `str.join`. +```py +colors = ['red', 'green', 'blue', 'yellow'] +separator = ", " +print(separator.join(colors)) # Prints 'red, green, blue, yellow' +``` +This method is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, +you must convert each element to a string before joining. +```py +integers = [1, 3, 6, 10, 15] +print(", ".join(str(e) for e in integers)) # Prints '1, 3, 6, 10, 15' +``` -- cgit v1.2.3 From 8baee2e1f04df52a87f620a8c004028cecdc9e39 Mon Sep 17 00:00:00 2001 From: SavagePastaMan <69145546+SavagePastaMan@users.noreply.github.com> Date: Mon, 12 Apr 2021 18:00:23 -0400 Subject: Be more consistent with word choice. Changed "method" and "way" to "solution" --- bot/resources/tags/str-join.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/resources/tags/str-join.md b/bot/resources/tags/str-join.md index e8407ac26..6390db9e5 100644 --- a/bot/resources/tags/str-join.md +++ b/bot/resources/tags/str-join.md @@ -9,15 +9,15 @@ for color in colors: output += color + separator print(output) # Prints 'red, green, blue, yellow, ' ``` -However, this solution is flawed. The separator is still added to the last color, and it is slow. +However, this solution is flawed. The separator is still added to the last element, and it is slow. -The better way is to use `str.join`. +The better solution is to use `str.join`. ```py colors = ['red', 'green', 'blue', 'yellow'] separator = ", " print(separator.join(colors)) # Prints 'red, green, blue, yellow' ``` -This method is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, +This solution is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, you must convert each element to a string before joining. ```py integers = [1, 3, 6, 10, 15] -- cgit v1.2.3 From 1bda27d3d93f9361baec25df37b39b2d2dd1c4b9 Mon Sep 17 00:00:00 2001 From: SavagePastaMan <69145546+SavagePastaMan@users.noreply.github.com> Date: Mon, 12 Apr 2021 18:40:53 -0400 Subject: Move comments to their own line. Previously the inline comments would wrap onto their own line which looked terrible. --- bot/resources/tags/str-join.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bot/resources/tags/str-join.md b/bot/resources/tags/str-join.md index 6390db9e5..a6b8fb793 100644 --- a/bot/resources/tags/str-join.md +++ b/bot/resources/tags/str-join.md @@ -7,7 +7,8 @@ output = "" separator = ", " for color in colors: output += color + separator -print(output) # Prints 'red, green, blue, yellow, ' +print(output) +# Prints 'red, green, blue, yellow, ' ``` However, this solution is flawed. The separator is still added to the last element, and it is slow. @@ -15,11 +16,13 @@ The better solution is to use `str.join`. ```py colors = ['red', 'green', 'blue', 'yellow'] separator = ", " -print(separator.join(colors)) # Prints 'red, green, blue, yellow' +print(separator.join(colors)) +# Prints 'red, green, blue, yellow' ``` This solution is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, you must convert each element to a string before joining. ```py integers = [1, 3, 6, 10, 15] -print(", ".join(str(e) for e in integers)) # Prints '1, 3, 6, 10, 15' +print(", ".join(str(e) for e in integers)) +# Prints '1, 3, 6, 10, 15' ``` -- cgit v1.2.3 From 52be2e92cfe62a0ed35811ec73e22e3e63275e88 Mon Sep 17 00:00:00 2001 From: Xithrius <15021300+Xithrius@users.noreply.github.com> Date: Mon, 3 May 2021 16:14:30 -0700 Subject: Removed opinions from text. Co-authored-by: Boris Muratov <8bee278@gmail.com> --- bot/resources/tags/str-join.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/resources/tags/str-join.md b/bot/resources/tags/str-join.md index a6b8fb793..c835f9313 100644 --- a/bot/resources/tags/str-join.md +++ b/bot/resources/tags/str-join.md @@ -1,6 +1,6 @@ **Joining Iterables** -Suppose you want to nicely display a list (or some other iterable). The naive solution would be something like this. +If you want to display a list (or some other iterable), you can write: ```py colors = ['red', 'green', 'blue', 'yellow'] output = "" @@ -10,16 +10,16 @@ for color in colors: print(output) # Prints 'red, green, blue, yellow, ' ``` -However, this solution is flawed. The separator is still added to the last element, and it is slow. +However, the separator is still added to the last element, and it is relatively slow. -The better solution is to use `str.join`. +A better solution is to use `str.join`. ```py colors = ['red', 'green', 'blue', 'yellow'] separator = ", " print(separator.join(colors)) # Prints 'red, green, blue, yellow' ``` -This solution is much simpler, faster, and solves the problem of the extra separator. An important thing to note is that you can only `str.join` strings. For a list of ints, +An important thing to note is that you can only `str.join` strings. For a list of ints, you must convert each element to a string before joining. ```py integers = [1, 3, 6, 10, 15] -- cgit v1.2.3