continuous onto function from irrationals in [0,1] onto rationals in [0,1]

Give a continuous surjective function from the irrationals in $[0,1]$ onto the rationals in $[0,1]$. Can we at least prove the existence of such a function? I couldn't see a function off the top of my head. Here we assume $[0,1]\setminus\mathbb Q$ with its usual metric.


Let $I = [0,1]\setminus \mathbb{Q}$, the set of irrationals in the unit interval.

Partition $I$ into countably many nonempty pieces, each given by the intersection of an open set in $[0,1]$ and $I$, such that $I = \cup_{k=0}^\infty I_k$. For example,

$I_0 = \left(\frac{1}{2},1\right) \cap I$,

$I_1 = \left(\frac{1}{4},\frac{1}{2}\right)\cap I$,

and in general $I_k = \left(\frac{1}{2^{k+1}},\frac{1}{2^k}\right)\cap I$. Note that each $I_k$ is open in $I$.

Enumerate $\mathbb{Q} \cap [0,1]$ (in any way you like): $\{q_0, q_1, q_2,\dots\}$.

For any $x\in I$, $x\in I_k$ for exactly one $k$. Define $f(x) = q_k$.

Note that $f$ is surjective, since each $I_k$ is nonempty.

We want to show that $f$ is continuous. Let $O\subseteq \mathbb{Q}\cap [0,1]$ be an open set. We can write $O = \bigcup_{q_k\in O} \{q_k\}$.

$$f^{-1}[O] = \bigcup_{q_k\in O}\,f^{-1}[\{q_k\}] = \bigcup_{q_k\in O} I_k.$$

This is a union of open sets in $I$, so it is open.

Note that we didn't actually use that $O$ was an open set: our function $f$ is continuous even if we give $\mathbb{Q}$ the discrete topology!


The function $x\mapsto\left\lfloor\frac1x\right\rfloor$ maps the irrationals in $[0,1]$ continuously onto the countable discrete space $\mathbb N=\{1,2,3,\dots\}$, which can be mapped continuously onto any countable (nonempty) topological space, such as the space $\mathbb Q\cap[0,1]$.