If you are reading from stdin, you can do this:
import sys
def ints():
for line in sys.stdin:
rtr=[int(d) for d in line.split() if d.isdigit()]
if rtr:
for n in rtr:
yield n
print(list(ints()))
Or, if your Py 3.3+ supports generator delegation:
import sys
def ints():
for line in sys.stdin:
yield from (int(d) for d in line.split() if d.isdigit())
You can also use the very slick fileinput which has the advantage of supporting both a file name (and the file by that name will then be opened and read) or input from the redirected stdin:
import fileinput
def ints():
for line in fileinput.input():
yield from (int(d) for d in line.split() if d.isdigit())
(If you test fileinput directly or manually, know that you need to end the input with two CtlD's -- not just one....)