How can i do this in Python?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Jan 25 04:59:54 EST 2007


Gabriel Genellina:
> import operator,fileinput
> mapper = map(operator.itemgetter, [0,1,20,21,2,10,12,14,11,4,5,6])
> for line in fileinput.input():
>      fields = line.split()
>      print '\t'.join(m(fields) for m in mapper)

I'm just curious, what's the advantage of using itemgetter there
compared to something simpler like this (untested!)?

import fileinput
positions = [0,1,20,21,2,10,12,14,11,4,5,6]
for line in fileinput.input():
    fields = line.split()
    print '\t'.join(fields[m] for m in positions)

Bye,
bearophile




More information about the Python-list mailing list