From: Francesc A. <fa...@gm...> - 2012-03-22 22:46:00
|
On 3/22/12 1:59 PM, Francesc Alted wrote: > On 3/22/12 12:48 PM, sreeaurovindh viswanathan wrote: >> But.. Can i get sort one column by descending and the other ascending. >> say >> if i have two columns and first i would like to sort the one in >> ascending and then sort the second column based on the search from the >> first. >> >> >> I mean I i have >> >> 1 5 >> 2 6 >> 1 8 >> 2 9 >> >> Could i get an output as >> >> 1 5 >> 1 8 >> 2 6 >> 2 9 > No, this is not supported by PyTables. > > But hey, you can always make use of the sorted iterator, and the > additonal sorting by yourselves. In your example, let's suppose that > column 0 is named 'f0' and column 1 is named 'f1'. Then, the next loop: > > prevval = None > gf1 = [] > for r in t.itersorted('f0'): > if r['f0'] != prevval: > if gf1: > gf1.sort() > print prevval, gf1[::-1] # reverse sorted > prevval = r['f0'] > gf1 = [] > gf1.append(r['f1']) > if gf1: > gf1.sort() > print prevval, gf1[::-1] # reverse sorted Hmm, I just realized that there it is another, equivalent code that solves the same problem: def field_selector(row): return row['f0'] for field, rows_grouped_by_field in itertools.groupby(t.itersorted('f0'), field_selector): group = [ r['f1'] for r in rows_grouped_by_field ] group.sort() print '%s -> %s' % (field, group[::-1]) The performance of both is similar, so use whatever you find more useful. For the record, I'm attaching a couple of self-contained examples that exercises the different approaches. -- Francesc Alted |