My data is tab delimited and looks like this:
Name Count Sample
Dog .0001 1
Dog .00003 1
Dog .0001 2
Cat .0004 1
Cat .0002 1
Cat .0003 2
Cat .0002 2
After i define my variables unid as the first column merged with the 3rd column (ex Dog_1) and num as the Count for that line, i append each num into a dictionary under the unid (using Python 2.7), like so:
for line in K:
sp = line.split("\t")
name = sp[0]
unid = sp[3][:-2] +"_"+ sp[0]
num = int(Decimal(sp[1]))
if not dict1.has_key(unid):
dict1[unid] = []
dict1[unid].append(num)
I try to sum it with this:
dictTot = sum(dict1.values())
But i get this error message:
TypeError: unsupported operand type(s) for +: 'int' and 'list'
How can I sum these values such that I can retrieve Cat_1: .0006, Cat_2: .0005 etc?
Sorry everyone, as I know my ? is not great. But as stated by Jacob below,
“dictTot = sum(sum(value) for value in dict1.values())” sums all of the sums, but what I am looking for is to sum each group of values under each key independently so I can find out how many Cats there are in sample 1 and so on. Perhaps sum is not right for this? Sorry, as evident I am not a Python extraordinaire.
I basically rewrote the whole thing…
the final dict is
the sum is
the values are lists, so you want to loop them to sum individually.
EDIT
apparently now you want “Cat_1: .0006, Cat_2: .0005 etc”, so upon
dict1, you can donow
dict1becomes