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

Discussion in 'Python' started by Ping-Hsun Hsieh, Nov 18, 2009.

  1. 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
     
    Ping-Hsun Hsieh, Nov 18, 2009
    #1
    1. Advertising

  2. Ping-Hsun Hsieh

    Jon Clements Guest

    On Nov 18, 8:57 pm, Ping-Hsun Hsieh <> 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.
     
    Jon Clements, Nov 19, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    8
    Views:
    446
    jerry
    Nov 19, 2003
  2. Krishna
    Replies:
    0
    Views:
    527
    Krishna
    Jul 13, 2006
  3. csgraham74

    Add two buttons in datagrid row in same column

    csgraham74, Jun 1, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    183
    Eliyahu Goldin
    Jun 2, 2005
  4. datagrid having row header and column header

    , Jul 13, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    187
  5. Replies:
    21
    Views:
    470
    Daniel Crichton
    Jan 19, 2009
Loading...

Share This Page