Dictionary of Dicts question

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Oct 17 09:34:13 EDT 2008


MRAB:
> for line in open(path):
>     fields = line.split("\t")
>     data[tuple(fields[ : 2])] = fields[2 : ]

Keeping the key as a string may have some memory/performance
advantages (not tested):

for line in open(path):
    fields = line.split("\t")
    data[fields[0] + fields[1]] = map(float, islice(fields, 2, None))

Or probably faster (not tested):

for line in open(path):
    parts = s.rsplit("\t", 6)
    data[parts[0]] = map(float, islice(parts, 1, None))

Or (not tested):

for line in open(path):
    parts = s.rsplit("\t", 6)
    data[parts[0]] = [float(parts[i]) for i in xrange(1, 7)]

Having a built-in xsplit/xsplitr method here probably helps
significantly.
If the FP numbers are really precise then you can compare them as
strings too, but that's quite unsafe.

Bye,
bearophile



More information about the Python-list mailing list