make two tables having same orders in both column and row names

Emile van Sebille emile at fenx.com
Thu Nov 19 16:38:00 EST 2009


On 11/18/2009 12:57 PM Ping-Hsun Hsieh said...
> Hi,
> 
> I would like to compare values in two table with same column and row names, but with different orders in column and row names.
> For example, table_A in a file looks like the follows:
> AA100	AA109	AA101	AA103	AA102
> BB1	2	9	2.3	1	28
> BB3	12	9	2.3	1	28
> BB9	0.5	2	2.3	1	28
> BB2	2	9	21	1	20
> 
> Table_B in the other file looks like the follows:
> AA101	AA109	AA100	AA103	AA102
> BB1	2	9	2.3	2	28
> BB2	2	9	2.3	1	28
> BB9	2	9	2.3	1	28
> BB3	2	2	2	1	28
> 
> Can anyone give an efficient way to make the two tables having same orders in column and row names so I can easily and correctly compare the values in positions?
> 
> Thanks,
> PingHsun
> 

This is one way... (python 2.4.1)
#For example, table_A in a file looks like the follows:
table_A = [ line.split()
   for line in """AA100  AA109   AA101   AA103   AA102
BB1 2   9   2.3 1   28
BB3 12  9   2.3 1   28
BB9 0.5 2   2.3 1   28
BB2 2   9   21  1   20""".split('\n')
   ]

#Table_B in the other file looks like the follows:
table_B = [ line.split()
   for line in """AA101  AA109   AA100   AA103   AA102
BB1 2   9   2.3 2   28
BB2 2   9   2.3 1   28
BB9 2   9   2.3 1   28
BB3 2   2   2   1   28""".split('\n')
   ]


for table in table_A,table_B:
     table[:] = [['']+table[0]]+sorted(table[1:])
     table = zip(*sorted(zip(*table)))
     for ii in table: print ii


Emile




More information about the Python-list mailing list