Comprehensions
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A list comprehension builds a new list from an old one in a single expression: square brackets around an expression, a for clause and an optional if clause. Step through big = [n * 10 for n in nums if n > 2] one item at a time: watch for take each value, if test it, and the expression turn the ones that pass into new items, so the new list grows to [40, 50]. Then see the same work written as a plain for loop.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What list does [n * 2 for n in [1, 2, 3]] build?
The for clause takes 1, 2 and 3 in turn, and the expression n * 2 is worked out for each one: [2, 4, 6].
02What does [n for n in [5, 1, 8, 3] if n > 4] give?
The if clause keeps only the items whose test is True. 5 > 4 and 8 > 4 are True; 1 and 3 are dropped, and the order of the original list is kept.
03For each item, in what order does Python run the parts of [n * 10 for n in nums if n > 2] ?
The expression is written first but runs last: for gives the item a name, if tests it, and only an item that passes reaches the expression.
04After big = [n * 10 for n in nums if n > 2] runs, what is nums?
A comprehension builds a brand-new list and gives it to big. The list it reads from is not changed.
05Which for loop builds the same list as big = [n * 10 for n in nums if n > 2] ?
Start with an empty list, walk through nums, test each item, and append the expression for the ones that pass. One difference: after the loop the name n is still set (to the last item); after the comprehension it is not.
المسرد
- List comprehension
- A short way to build a new list from another: [expression for name in list if test], inside square brackets.
- Expression
- The part written first, such as n * 10: what each kept item becomes in the new list.
- for clause
- for n in nums: takes the items of a list one at a time, left to right, and gives each the name n.
- if clause
- An optional filter, such as if n > 2: only items whose test is True reach the expression.
- Index
- An item's position in a list, counting from 0: nums[0] is the first item.
- append()
- A list method that adds one item to the end of a list; a comprehension does this for every kept item.
- Dict and set comprehensions
- The same idea in curly braces: {n: n * 10 for n in nums} builds a dict, {n % 2 for n in nums} a set.
الملاحظات والمصادر
- The comprehension is split over four lines so each part gets its own line; Python joins lines inside brackets, so it is the same as big = [n * 10 for n in nums if n > 2].
- For each item Python runs the clauses in the order for, then if, then the expression, even though the expression is written first.
- In Python 3 a comprehension runs in its own small scope, so its loop name (n here) does not leak out; a plain for loop does leave n set to the last item.
- A list comprehension always builds a brand-new list; the list it reads from (nums) is not changed.
- Curly braces make a set comprehension, {n % 2 for n in nums}, or with key: value a dict comprehension, {n: n * 10 for n in nums}.
- Python tutorial: Data structures, list comprehensions
- Python reference: Displays for lists, sets and dictionaries
- Python reference: List displays
- Python tutorial: More on lists (append)
- Python tutorial: An informal introduction, lists and indexes
- Python reference: Implicit line joining
- Python library: Comparisons
- Python glossary: list comprehension
اختبار يتاح بعد الخطوة الأخيرة.