"groupby" is brilliant!

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue Jun 13 04:28:57 EDT 2006


>
> reader = csv.reader(open('trans.csv', 'rb'))
> rows = []
> for row in reader:
>     rows.append(row)
>

This is untested, but you might think about converting your explicit "for...
append" loop into either a list comp,

    rows = [row for row in reader]

or just a plain list constructor:

    rows = list(reader)

Neh?

-- Paul


(Oh, and I like groupby too!  Combine it with sort to quickly create
histograms.)

# tally a histogram of a list of values from 1-10
dataValueRange = range(1,11)
data = [random.choice(dataValueRange) for i in xrange(10000)]

hist = [ (k,len(list(g))) for k,g in itertools.groupby(sorted(data)) ]
print hist

histAsDict = dict((k,len(list(g))) for k,g in
itertools.groupby(sorted(data)))
print histAsDict

Gives:

[(1, 979), (2, 1034), (3, 985), (4, 969), (5, 1020), (6, 975), (7, 981), (8,
1070), (9, 1003), (10, 984)]
{1: 979, 2: 1034, 3: 985, 4: 969, 5: 1020, 6: 975, 7: 981, 8: 1070, 9: 1003,
10: 984}





More information about the Python-list mailing list