i have next problem:
x=[['1', '7', 'u1'], ['1.5', '8', 'u1']] y=sum(sum(float(el) el in els[:-1]) els in x) print(x) print(y)
in code sum, sum numbers, want sum first ['1', '7', 'u1'], first number, , second ['1.5', '8', 'u1'] first number, , same second...
so final result fill "matrix" :
y= [ [2.5], #1+1.5=2.5 [15]] #7+8=15
>>> x=[['1', '7', 'u1'], ['1.5', '8', 'u1']] >>> zip(*x) [('1', '1.5'), ('7', '8'), ('u1', 'u1')] >>> [[sum(float(n) n in nums)] nums in zip(*x)[:-1]] [[2.5], [15.0]]
zip(*x)
simple way transpose matrix (switch rows <--> columns), , allows sum each row.
Comments
Post a Comment