我同意评论和数据结构的其他答案中所说的一切。我只添加了这个答案,因为它提供了一种以OP请求的格式获取表格的方法。
EDIT注释掉了Counter的使用,以便在Python 2.6上使用它
# from collections import Counter
from pprint import pprint
table = [['Bacteria', 'Aquificae', 'Persephonella'],
['Bacteria', 'Aquificae', 'Thermovibrio'],
['Bacteria', 'Firmicutes', 'Bacillus']]
# count_dict = Counter( [ item for row in table for item in row ] )
count_dict = {}
for row in table:
for item in row:
count_dict[item] = count_dict.get(item, 0) + 1
for index_row, row in enumerate(table):
for index_col, element in enumerate(row):
if element in count_dict:
table[index_row][index_col] = '%s %s' % (element, count_dict[element])
del count_dict[element]
else:
table[index_row][index_col] = '-'
pprint(table)产生:
[['Bacteria 3', 'Aquificae 2', 'Persephonella 1'],
['-', '-', 'Thermovibrio 1'],
['-', 'Firmicutes 1', 'Bacillus 1']]