Jay - 11 months ago 46

Python Question

I've solved the problem with the code below, but I'm trying to understand a better way to strip out the zeros at the end of the each string of numbers because they throw off the calculation. The solution I have seems heavy handed, is there a more elegant way to solve this without a for loop removing the 0s?

Determine the average of a series of numbers like this:

97 143 3 149 1 181 195 76 0

8042 4302 1273 3858 4019 8051 4831 7747 4887 768 6391 7817 2635 6203 0

1783 218 154 360 409 929 1503 428 73 1505 1868 1625 64 1613 0

`import sys`

inp = sys.stdin.readlines()

lin = []

for i in inp[1:]:

lin.append(map(int,i.split()))

/---Crux----

`for r in lin:`

del r[-1]

/-------------

`for n in lin:`

print int(round((sum(n) / float(len(n))))),

Any other critiques and constructive criticisms are appreciated.

Answer Source

You can do it all in one go using slicing and list comprehension:

```
lin = [map(int,i.split())[:-1] for i in inp[1:]]
```