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

Commitc283dbd

Browse files
committed
Reword the discussion on short ways to manipulate lists
- Remove emphasis on map and filter, in favor of generator expressions. - Move list comprehensions and generator expressions under "Short ways to manipulate lists" rather than "Filtering a list". - Add some examples.
1 parent90af77a commitc283dbd

File tree

1 file changed

+66
-51
lines changed

1 file changed

+66
-51
lines changed

‎docs/writing/style.rst

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ Conventions
521521
Here are some conventions you should follow to make your code easier to read.
522522

523523
Check if a variable equals a constant
524-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
524+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
525525

526526
You don't need to explicitly compare a value to True, or None, or 0 -- you can
527527
just add it to the if statement. See `Truth Value Testing
@@ -588,80 +588,104 @@ Short Ways to Manipulate Lists
588588

589589
`List comprehensions
590590
<http://docs.python.org/tutorial/datastructures.html#list-comprehensions>`_
591-
provide a powerful, concise way to work with lists. Also, the:py:func:`map` and
592-
:py:func:`filter` functions can perform operations on lists using a different,
593-
more concise syntax.
591+
provide a powerful, concise way to work with lists.
594592

595-
Filtering a list
596-
~~~~~~~~~~~~~~~~
593+
`Generator expressions
594+
<http://docs.python.org/tutorial/classes.html#generator-expressions>`_
595+
follow almost the same syntax as list comprehensions but return a generator
596+
instead of a list.
597597

598-
**Bad**:
598+
Creating a new list requires more work and uses more memory. If you are just going
599+
to loop through the new list, prefer using an iterator instead.
599600

600-
Never remove items from a list while you are iterating through it.
601+
**Bad**:
601602

602603
..code-block::python
603604
604-
# Filter elements greater than 4
605-
a= [3,4,5]
606-
for iin a:
607-
if i>4:
608-
a.remove(i)
605+
# needlessly allocates a list of all (gpa, name) entires in memory
606+
valedictorian=max([(student.gpa, student.name)for studentin graduates])
609607
610-
Don't make multiple passes through the list.
608+
**Good**:
611609

612610
..code-block::python
613611
614-
while iin a:
615-
a.remove(i)
612+
valedictorian=max((student.gpa, student.name)for studentin graduates)
613+
614+
615+
Use list comprehensions when you really need to create a second list, for
616+
example if you need to use the result multiple times.
617+
618+
619+
If your logic is too complicated for a short list comprehension or generator
620+
expression, consider using a generator function instead of returning a list.
616621

617622
**Good**:
618623

619-
Python has a few standard ways of filtering lists.
620-
The approach you use depends on:
624+
..code-block::python
625+
626+
defmake_batches(items,batch_size):
627+
"""
628+
>>>list(make_batches([1, 2, 3, 4, 5], batch_size=3))
629+
[[1, 2, 3], [4, 5]]
630+
"""
631+
current_batch= []
632+
for itemin items:
633+
current_batch.append(item)
634+
iflen(current_batch)== batch_size:
635+
yield current_batch
636+
current_batch= []
637+
yield current_batch
621638
622-
* Python 2.x vs. 3.x
623-
* Lists vs. iterators
624-
* Possible side effects of modifying the original list
625639
626-
Python 2.x vs. 3.x
627-
::::::::::::::::::
640+
Never use a list comprehension just for its side effects.
628641

629-
Starting with Python 3.0, the:py:func:`filter` function returns an iterator instead of a list.
630-
Wrap it in:py:func:`list` if you truly need a list.
642+
**Bad**:
631643

632644
..code-block::python
633645
634-
list(filter(...))
646+
[print(x)for xin seqeunce]
635647
636-
List comprehensions and generator expressions work the same in both 2.x and 3.x (except that comprehensions in 2.x "leak" variables into the enclosing namespace):
648+
**Good**:
637649

638-
* Comprehensions create a new list object.
639-
* Generators iterate over the original list.
650+
..code-block::python
640651
641-
The:py:func:`filter` function:
652+
for xin sequence:
653+
print(x)
642654
643-
* in 2.x returns a list (use itertools.ifilter if you want an iterator)
644-
* in 3.x returns an iterator
645655
646-
Lists vs. iterators
647-
:::::::::::::::::::
656+
Filtering a list
657+
~~~~~~~~~~~~~~~~
658+
659+
**Bad**:
660+
661+
Never remove items from a list while you are iterating through it.
662+
663+
..code-block::python
664+
665+
# Filter elements greater than 4
666+
a= [3,4,5]
667+
for iin a:
668+
if i>4:
669+
a.remove(i)
670+
671+
Don't make multiple passes through the list.
672+
673+
..code-block::python
674+
675+
while iin a:
676+
a.remove(i)
677+
678+
**Good**:
648679

649-
Creating anewlistrequires more work and uses more memory. If you a just going to loop through the new list, consider using an iterator instead.
680+
Use a listcomprehension or generator expression.
650681

651682
..code-block::python
652683
653684
# comprehensions create a new list object
654685
filtered_values= [valuefor valuein sequenceif value!= x]
655-
# Or (2.x)
656-
filtered_values=filter(lambdai: i!= x, sequence)
657686
658687
# generators don't create another list
659688
filtered_values= (valuefor valuein sequenceif value!= x)
660-
# Or (3.x)
661-
filtered_values=filter(lambdai: i!= x, sequence)
662-
# Or (2.x)
663-
filtered_values= itertools.ifilter(lambdai: i!= x, sequence)
664-
665689
666690
667691
Possible side effects of modifying the original list
@@ -673,10 +697,6 @@ Modifying the original list can be risky if there are other variables referencin
673697
674698
# replace the contents of the original list
675699
sequence[::]= [valuefor valuein sequenceif value!= x]
676-
# Or
677-
sequence[::]= (valuefor valuein sequenceif value!= x)
678-
# Or
679-
sequence[::]=filter(lambdavalue: value!= x, sequence)
680700
681701
682702
Modifying the values in a list
@@ -705,11 +725,6 @@ It's safer to create a new list object and leave the original alone.
705725
706726
# assign the variable "a" to a new list without changing "b"
707727
a= [i+3for iin a]
708-
# Or (Python 2.x):
709-
a=map(lambdai: i+3, a)
710-
# Or (Python 3.x)
711-
a=list(map(lambdai: i+3, a))
712-
713728
714729
Use:py:func:`enumerate` keep a count of your place in the list.
715730

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp