Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > make two tables having same orders in both column and row names

Reply
Thread Tools

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

 
 
Ping-Hsun Hsieh
Guest
Posts: n/a
 
      11-18-2009
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

 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-19-2009
On Nov 18, 8:57*pm, Ping-Hsun Hsieh <hsi...@ohsu.edu> wrote:
> 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


Use a dictionary with a tuple of the row 'name' and column 'name' as
the key.

The following was put together in a hurry, so take with a pinch of
salt (and brandy or something )...

t1data = """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"""


def create_table(what):
from itertools import imap, islice, izip, cycle, repeat
table = filter(None, imap(str.split, what.split('\n')))
table_dict = {}
for cols in islice(table, 1, None):
for row_name, col_name, col in izip(cycle(table[0]), repeat
(cols[0]), islice(cols, 1, None)):
table_dict[(row_name, col_name)] = col
return table_dict


print create_table(t1data)


hth
Jon.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 Gridview. Dropdown A is column from database, Dropdown B is column from database, Data in A and B must be from same row. anonymoushamster@gmail.com ASP .Net 2 11-07-2007 12:40 PM
datagrid having row header and column header krishna.dwivedi@gmail.com ASP .Net Datagrid Control 0 07-13-2006 08:20 AM
Datagrid - each item having two headers one displayed by row another by column Krishna ASP .Net 0 07-13-2006 07:36 AM
Add two buttons in datagrid row in same column csgraham74 ASP .Net Datagrid Control 1 06-02-2005 09:54 AM
How to call same instance methods in different orders lonelyplanet999 Java 8 11-19-2003 10:25 PM



Advertisments