Todos os livros

Profissional

Apps Sobre Coach Entrar Começar a ler

Computer Science · Glossário

O que é Comprehension?

Definition 2.7 High School Computer Science · Capítulo 2 — Tuples, Sets and Comprehensions

A comprehension is an expression that builds a list, a set or a dictionary from a sequence: [e for x in t] is the list of the values of the expression e for each element x of t, in order; [e for x in t if c] keeps only the elements for which the condition c is true. With braces, {e for x in t} builds a set and {k: v for x in t} a dictionary.

A comprehension is read from the middle: the loop, then the condition, then the expression, which comes first in the writing. On the right, the loop it stands for.
A comprehension is read from the middle: the loop, then the condition, then the expression, which comes first in the writing. On the right, the loop it stands for.

Exemplos

Example 2.8 (The books of a member, in one line)

The function books_of of the town listing is the set comprehension {loan["book_id"] for loan in loans if loan["member_id"] == member}: for each loan, if it is the member’s, put its book in the set.

Ler no capítulo →