Skip to main content
7 of 8
Additional methods and time measurement.
DevLounge
  • 208
  • 1
  • 7

So here is what I came up with, now that I had access to a computer:

raw_data.csv:

20110117559515,1,10,faa
20110117559515,2,20,bar
20110117559515,3,30,baz
20110117559572,4,40,fii
20110117559572,5,50,bir
20110117559572,6,60,biz
20110117559574,7,70,foo
20110117559587,8,80,bor
20110117559588,9,90,boz

code:

import csv
from collections import defaultdict
from pprint import pprint
import timeit


def method1():
    rows = list(csv.reader(open('raw_data.csv', 'r'), delimiter=','))
    cols = zip(*rows)
    unik = set(cols[0])
    
    indexed = defaultdict(list)
    
    for x in unik:
        i = cols[0].index(x)
        indexed[i] = rows[i]
    
    return indexed

def method2():
    rows = list(csv.reader(open('raw_data.csv', 'r'), delimiter=','))
    cols = zip(*rows)
    unik = set(cols[0])
    
    indexed = defaultdict(list)
    
    for x in unik:
        i = next(index for index,fid in enumerate(cols[0]) if fid == x)
        indexed[i] = rows[i]

    return indexed

def method3():
    rows = list(csv.reader(open('raw_data.csv', 'r'), delimiter=','))
    cols = zip(*rows)
    indexes = [cols[0].index(x) for x in set(cols[0])]
    
    for index in indexes:
        yield (index,rows[index])


if __name__ == '__main__':
    
    results = method1()    
    print 'indexed:'
    pprint(dict(results))
    
    print '-' * 80
    
    results = method2()    
    print 'indexed:'
    pprint(dict(results))
    
    print '-' * 80
    
    results = dict(method3())
    print 'indexed:'
    pprint(results)
    
    #--- Timeit ---
    
    print 'method1:', timeit.timeit('method1()', setup="from __main__ import method1", number=10000)
    print 'method2:', timeit.timeit('method2()', setup="from __main__ import method2", number=10000)
    print 'method3:', timeit.timeit('method3()', setup="from __main__ import method3", number=10000)

Output:

indexed:
{0: ['20110117559515', '1', '10', 'faa'],
 3: ['20110117559572', '4', '40', 'fii'],
 6: ['20110117559574', '7', '70', 'foo'],
 7: ['20110117559587', '8', '80', 'bor'],
 8: ['20110117559588', '9', '90', 'boz']}
--------------------------------------------------------------------------------
indexed:
{0: ['20110117559515', '1', '10', 'faa'],
 3: ['20110117559572', '4', '40', 'fii'],
 6: ['20110117559574', '7', '70', 'foo'],
 7: ['20110117559587', '8', '80', 'bor'],
 8: ['20110117559588', '9', '90', 'boz']}
--------------------------------------------------------------------------------
indexed:
{0: ['20110117559515', '1', '10', 'faa'],
 3: ['20110117559572', '4', '40', 'fii'],
 6: ['20110117559574', '7', '70', 'foo'],
 7: ['20110117559587', '8', '80', 'bor'],
 8: ['20110117559588', '9', '90', 'boz']}
method1: 0.272489070892
method2: 0.366016149521
method3: 0.00432395935059
DevLounge
  • 208
  • 1
  • 7