将值收集到列表字典中;一个collections.defaultdict()
对象是最简单的:
from collections import defaultdict
import csv
species = defaultdict(list)
with open('speciesandbss.csv', 'rb') as inputfile:
for row in csv.reader(inputfile):
species[row[0]].append(row[1])
for name in sorted(species, key=str.lower):
print '{} = {}'.format(name, species[name])
输出:
acutata = ['0.16509', '0.16509', '0.16509']
acutocostata = ['0.03145', '0.01936', '0.01781', '0.01698', '0.01684', '0.01077']
adkinsi = ['0.16509']
Aenona = ['0.01311', '0.01311']
aequilateralis = ['0.00495', '0.00445', '0.00368', '0.00356']
agdjakendensis = ['0.00628']
Agerostrea = ['0.01764']
albertensis = ['0.00852', '0.00356', '0.00495', '0.00461', '0.00445', '0.0041']
alta = ['0.00328', '0.33148', '0.33148', '0.43129', '0.33148', '0.325', '0.17882', '0.00307']
alternata = ['0.04929', '0.03373', '0.01311']
americana = ['0.01497', '0.00436', '0.01497', '0.00495', '0.00461', '0.00445', '0.00105']
anacachoensis = ['0.05696', '0.05696', '0.05172', '0.03373']
angulatum = ['0.01179']
anomala = ['0.00852']
Anomia = ['0.00852', '0.00506', '0.02955', '0.00786']
anteradiata = ['0.43129', '0.16509']
antroea = ['0.01373']
antrosa = ['0.01103']
Aphrodina = ['0.43129', '0.01311']
apressus = ['0.01564']
Arca = ['0.01179', '0.01311', '0.01311', '0.01311', '0.01224', '0.01224']
archeri = ['0.16509', '0.16509', '0.16509', '0.16509', '0.16509', '0.16509', '0.16509']
Arctica = ['0.00203']
argentaria = ['0.01233', '0.33148', '0.33148', '0.33148', '0.33148', '0.43129', '0.43129', '0.21502', '0.01311', '0.01224', '0.00352', '0.01311', '0.01311', '0.01179', '0.01373', '0.01311', '0.01311', '0.01224', '0.01224', '0.01224', '0.01224', '0.16509', '0.01564']
armatum = ['0.33148', '0.33148', '0.33148', '0.33148']
Ascaulocardium = ['0.43129', '0.21502']
assiniboiensis = ['0.00401', '0.00436', '0.00436', '0.00685', '0.00495', '0.00495', '0.00486', '0.00461', '0.00453', '0.00445']
assiniboinensis = ['0.00117']
Astarte = ['0.01497', '0.01311']
balchii = ['0.00786']
balticus = ['0.05696', '0.05696', '0.05696', '0.05238', '0.03623', '0.03373', '0.00724', '0.04574']
barabini = ['0.01233', '0.00852', '0.00506']
Barbatia = ['0.05696', '0.03373', '0.18121', '0.17882', '0.01224']
bartoni = ['0.16509']
bartrami = ['0.325', '0.26095', '0.25697', '0.17882', '0.01311']
bella = ['0.01311', '0.01311', '0.01311', '0.01311', '0.01311', '0.01311', '0.01311', '0.01311', '0.01764']
bellisculptus = ['0.05696', '0.05696', '0.03373', '0.25697', '0.01311', '0.01224', '0.01311', '0.01224', '0.01179', '0.01311', '0.01311', '0.01311', '0.01311', '0.01311']
berryi = ['0.43129', '0.17882']
biplicata = ['0.05696', '0.03373', '0.33148', '0.33148', '0.43129', '0.01224', '0.16509', '0.16509', '0.16509', '0.16509', '0.16509', '0.16509']
bisulcata = ['0.01311', '0.01224', '0.01224']
borealis = ['0.01233', '0.00852', '0.00452', '0.00401', '0.00852', '0.00452', '0.00401', '0.00436', '0.01497', '0.02971']
bowiei = ['0.16509', '0.16509']
Breviarca = ['0.01311', '0.01311']
Brevicardium = ['0.16509']
brevifrons = ['0.43129']
bryani = ['0.16509']
bulbosa = ['0.16509', '0.16509', '0.16509']
burlingtonensis = ['0.05696', '0.05696', '0.04929', '0.03373', '0.325', '0.01311', '0.01179', '0.04574']
等等
写入最低和最高值:
with open('outputfile.csv', 'wb') as outputfile:
writer = csv.writer(outputfile)
writer.writerows([n, min(values), max(values)] for n, v in species.iteritems() for values in (map(float, v),))