Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit90af77a

Browse files
committed
Remove explicit recommendation to use map and filter over list comprehensions
1 parent7375152 commit90af77a

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

‎docs/writing/structure.rst

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,12 @@ and can be used as a key for a dictionary.
809809

810810
One peculiarity of Python that can surprise beginners is that
811811
strings are immutable. This means that when constructing a string from
812-
its parts, it is much more efficient to accumulate the parts in a list,
813-
which is mutable, and then glue ('join') the parts together when the
814-
full string is needed. One thing to notice, however, is that list
815-
comprehensions are better and faster than constructing a list in a loop
816-
with calls to ``append()``.
817-
818-
One other option is using the map function, which can 'map' a function
819-
('str') to an iterable ('range(20)'). This results in a map object,
820-
which you can then ('join') together just like the other examples.
821-
The map function can be even faster than a list comprehension in some cases.
812+
its parts, appending each part to the string is inefficient because
813+
the entirety of the string is copied on each append.
814+
Instead, it is much more efficient to accumulate the parts in a list,
815+
which is mutable, and then glue (``join``) the parts together when the
816+
full string is needed. List comprehensions are usually the fastest and
817+
most idiomatic way to do this.
822818

823819
**Bad**
824820

@@ -830,7 +826,7 @@ The map function can be even faster than a list comprehension in some cases.
830826
nums+=str(n)# slow and inefficient
831827
print nums
832828
833-
**Good**
829+
**Better**
834830

835831
..code-block::python
836832
@@ -840,20 +836,12 @@ The map function can be even faster than a list comprehension in some cases.
840836
nums.append(str(n))
841837
print"".join(nums)# much more efficient
842838
843-
**Better**
844-
845-
..code-block::python
846-
847-
# create a concatenated string from 0 to 19 (e.g. "012..1819")
848-
nums= [str(n)for ninrange(20)]
849-
print"".join(nums)
850-
851839
**Best**
852840

853841
..code-block::python
854842
855843
# create a concatenated string from 0 to 19 (e.g. "012..1819")
856-
nums=map(str,range(20))
844+
nums=[str(n)for ninrange(20)]
857845
print"".join(nums)
858846
859847
One final thing to mention about strings is that using ``join()`` is not always

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp