python nested list: replace repeted items in nested list while retaining first occurrence
Question: I have a list of lists and want to replace all the occurrences of the same
word within the entire list/matrix with a dash except the first occurrence. I
have created a separate list that has a count of how many of each element is
in the original list of lists. I want the first occurrence of the word to be
replaced by the item in the count list so the number is there.

    
    
    table = [['Bacteria', 'Aquificae', 'Persephonella'],
            ['Bacteria', 'Aquificae', 'Thermovibrio'],
            ['Bateria', 'Firmicutes', 'Bacillus']]
    countlist = ['Bacteria3', 'Aquificae2', 'Persephonella1', 'Thermovibrio1', 'Firmicutes1', 'Bacillus1']
    

So I want the list to be like this when I'm done:

    
    
    table = [['Bacteria3', 'Aquificae2', 'Persephonella1'],
            ['-', '-', 'Thermovibrio1'],
            ['-', 'Firmicutes1', 'Bacillus1']]
    

I want to put this into a tab delimited table after so it won't look so
confusing in the end.

This is what I have currently for replacing the word with the count list
version but is not working:

    
    
    for num in range(1525):
        for n in table[num]:
                for s in count:
                        if n in s:
                                n = s
    

I'm stuck on this and any help would be greatly appreciated.

**EDIT** Working in Python 2.6.1


Answer: I can't tell if your table with dashes in is required, or just a step you
think you need to get to the tab delimitated table, this code will get a list
with names appended with totals that can be used to make the tab delimitated
table

    
    
    from collections import Counter
    count= Counter([item for sublist in table for item in sublist])
    totals= ["%s%i"%(e,c) for e,c in count.most_common()]
    
    #can then be tab deliminated
    "\t".join(totals)
    

