I am writing a small Python program for the use in our departments stockroom. This is what I have come up with (just starting):

# table of chemicals in stockroom
# order --> stock number, chemical name, quantity (grams)
# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500]
]
# pretty print the table
print '-'*66
print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
print '-'*66
for item in table:
    print "%-10s   %-40s  %8d" % (item[0], item[1], item[2])
print '-'*66

For right now I would like to sort this list of lists by stock number, chemical name and quantity on hand. Any ideas?

Here is one way you could sort your table by column. Also note that I put your pretty print into a function ...

# table of chemicals in stockroom as list of lists
# order --> stock number, chemical name, quantity (grams)

from operator import itemgetter

def sort_table(table, column_num):
    """
    sort a list of sublists that form a table by column number
    note that first column in table starts with column_num zero
    """
    return sorted(table, key=itemgetter(column_num))

def print_table(table):
    """
    pretty print the table
    """
    print '-'*66
    print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
    print '-'*66
    for item in table:
        print "%-10s   %-40s  %8d" % (item[SN], item[CN], item[QY])
    print '-'*66

# assign constants for the indexes
SN = 0  # index of stock number
CN = 1  # index of chemical name
QY = 2  # index of quantity

# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500]
]

print "original table:"
print_table(table)
print
print "table sorted by chemical name:"
table1 = sort_table(table, CN)
print_table(table1)

Your code works vegaseat, but I have a problem with it. If you know anything about chemistry, there are several chlorobenzoic acids, ortho, meta and para, indicating the position of the chlorine in relation to the carboxylic acid. I would like the sort to keep the chlorobenzoic acids together, like the original table in the expanded data below:

# table of chemicals in stockroom as list of lists
# order --> stock number, chemical name, quantity (grams)
from operator import itemgetter
def sort_table(table, column_num):
    """
    sort a list of sublists that form a table by column number
    note that first column in table starts with column_num zero
    """
    return sorted(table, key=itemgetter(column_num))
def print_table(table):
    """
    pretty print the table
    """
    print '-'*66
    print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
    print '-'*66
    for item in table:
        print "%-10s   %-40s  %8d" % (item[0], item[1], item[2])
    print '-'*66
# assign constants
SN = 0  # index of stock number
CN = 1  # index of chemical name
QY = 2  # index of quantity
# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99133', 'o-chlorobenzoic acid', 1500],
['ud-99139', 'p-chlorobenzoic acid', 600],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500],
['ud-99412', 'nickel sulfate', 100],
['ud-99463', 'oleic acid', 250]
]
print "original table:"
print_table(table)
print
print "table sorted by chemical name:"
table1 = sort_table(table, CN)
print_table(table1)

Does anyone have an idea how to do this?

Here is away to take care of chemical prefixes like 'o-', 'm-' and 'p-' when sorting. Just a little commonly used trick you can also use when sorting by your table columns:

# table of chemicals in stockroom as list of lists
# order --> stock number, chemical name, quantity (grams)

from operator import itemgetter

def sort_table(table, column_num):
    """
    sort a list of sublists that form a table by column number
    note that first column in table starts with column_num zero
    """
    return sorted(table, key=itemgetter(column_num))

def sort_chem(table):
    """
    this should take care of 'o-', 'm-' and 'p-' prefixes
    temporary change of sublist by adding modified chemical as first item
    sort this temp list and then rebuild by removing the temporary first item
    """
    temp = []
    for item in table:
        chem = item[CN]
        # removes leading 'o-' and adds '-o' to end for sort
        # you can add more similar prefixes here too
        if chem.startswith('o-'):
            chem = chem[2:] + '-o'
        if chem.startswith('m-'):
            chem = chem[2:] + '-m'
        if chem.startswith('p-'):
            chem = chem[2:] + '-p'        
        temp.append([chem,item])
    # this will sort by first item in each sublist
    temp.sort()
    new_table = []
    # rebuild the proper table
    for item in temp:
        #print item  # for test
        new_table.append(item[1])
    return new_table
 
def print_table(table):
    """
    pretty print the table
    """
    print '-'*66
    print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
    print '-'*66
    for item in table:
        print "%-10s   %-40s  %8d" % (item[0], item[1], item[2])
    print '-'*66

# assign constants
SN = 0  # index of stock number
CN = 1  # index of chemical name
QY = 2  # index of quantity

# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99133', 'o-chlorobenzoic acid', 1500],
['ud-99139', 'p-chlorobenzoic acid', 600],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500],
['ud-99412', 'nickel sulfate', 100],
['ud-99463', 'oleic acid', 250]
]

print "original table:"
print_table(table)
print
print "table sorted by chemical name:"
table1 = sort_table(table, CN)
print_table(table1)

print
print "table sorted by chemical name (modified sort):"
table2 = sort_chem(table)
print_table(table2)

Thank you Mr. Uran! I am still trying to think my way through the details of the sort_chem function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.