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

Simon Forman sajmikins at gmail.com
Thu Nov 19 23:48:11 CET 2009


On Wed, Nov 18, 2009 at 3:57 PM, Ping-Hsun Hsieh <hsiehp at 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

Depending on the kind of comparisons you want to do and the sizes of
your input files you could build a dict of dicts.
For example:


def generate_dict_values(F):
    column_names = f.readline().split()
    for line in F:
        row = line.split()
        yield row[0], dict(zip(column_names, row[1:]))


# Fake a file.
from StringIO import StringIO
f = StringIO('''\
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
''')


d = dict(generate_dict_values(f))

# Now you can access the values without regards to the order in the
file like so:

d['BB9']['AA109']



# "Pretty print" the dict of dicts.
from pprint import pprint
pprint(d)

# Prints:
#
#    {'BB1': {'AA100': '2',
#             'AA101': '2.3',
#             'AA102': '28',
#             'AA103': '1',
#             'AA109': '9'},
#     'BB2': {'AA100': '2',
#             'AA101': '21',
#             'AA102': '20',
#             'AA103': '1',
#             'AA109': '9'},
#     'BB3': {'AA100': '12',
#             'AA101': '2.3',
#             'AA102': '28',
#             'AA103': '1',
#             'AA109': '9'},
#     'BB9': {'AA100': '0.5',
#             'AA101': '2.3',
#             'AA102': '28',
#             'AA103': '1',
#             'AA109': '2'}}



More information about the Python-list mailing list