from StringIO import StringIO
input = """Site Species
A01 ACRB
A01 TBL
A02 TBL
A03 GRF
"""
counts = {}
sites = set()
species = set()
# Count pairs (site, specie)
for line in StringIO(input).readlines()[1:]:
site, specie = line.strip().split()
sites.add(site)
species.add(specie)
count = counts.get((site, specie), 0)
counts[(site, specie)] = count + 1
# Print first row.
print "Site\t",
for specie in species:
print specie, "\t",
print
# Print other rows.
for site in sites:
print site, "\t",
for specie in species:
print counts.get((site, specie), 0),
print
import itertools
l = [('A01', 'ACRB'), ('A01', 'TBL'), ('A02', 'TBL'), ('A03', 'GRF')]
def mygrouping(l):
speclist = list(set(i[1] for i in l))
yield tuple(speclist)
l.sort()
gr = itertools.groupby(l, lambda i:i[0]) # i[0] is the site; group by that...
for site, items in gr:
counts = [0] * len(speclist)
for _site, species in items:
counts[speclist.index(species)] += 1
yield site, tuple(counts)
print list(mygrouping(l))
from collections import Counter
with open("in.txt") as f:
next(f) # do this to skip the first row of the file
c = Counter(tuple(row.split()) for row in f if not row.isspace())
sites = sorted(set(x[0] for x in c))
species = sorted(set(x[1] for x in c))
print 'Site\t', '\t'.join(species)
for site in sites:
print site,'\t', '\t'.join(str(c[site, spec]) for spec in species)
让我们看看。。。在
另一个使用namedtuples的解决方案是
^{pr2}$展示的东西我会给你的。在
下面是一个用Python2.7实现的方法
相关问题 更多 >
编程相关推荐