In Python, list comprehension notation can be used to easily describe sophisticated lists succinctly. They are of the form
[expression(x) for x in iterable if predicate(x)]
Setting s = [1,2,3,4]
, examples might include the square of odd members
of s
or the sine of members of s
which are greater than 1.
[x*x for x in s if x % 2 == 1]
[sin(z) for z in s if z > 1]
This is clearly inspired by set builder notation in mathematics. However, one difference is the general format of set builder notation there is no for
statement. That is a typical set builder expression might be
$$\{\ f(x) : p(x)\ \}$$
where $f(x)$ is an expression dependent on $x$ and $p(x)$ is some predicate on $x$.
The syntax doesn't have a clear way of denoting the set from which $x$ came. Sometimes this is shoe-horned into the expression or predicate $$a\mathbb{Z} = \{\ an : n \in \mathbb{Z}\ \}$$ $$\mathbb{N} = \{\ n \in \mathbb{Z} : n > 0 \ \}$$ $$X = \{\ mn : m \in \mathbb{Z} \wedge n \in \mathbb{Z} \wedge n > 0\ \wedge m < 5\}$$
I would like to know if there is an established notation that serves
the same role as for
? I have taken to writing in my notes
$$X = \{\ mn : m \in \mathbb{Z} \wedge n \in \mathbb{Z} : n > 0\ \wedge m < 5\}$$
but this is an invention not convention.
This feels like a stupid question, but sticking the $x \in S$ statement into the predicate feels almost like $x$ is being selected from a universal set then whittled down to being contained in $S$, which must be wrong.