Tuesday 6 July 2010

Python input to lists..

Trying to stream a file into python and then convert each line by splitting on whitespace to a token list for parsing word by word.

I started out fairly verbose.

tokens= list()
lines= sys.stdin.readlines()
for line in lines:
tokens.append(line.split())


And ended up reducing it to a one liner.


inplist = list(i.split() for i in sys.stdin.readlines())

No comments:

Post a Comment