-1

I have a data with two columns as shown below. i am trying to estimate a standard deviation of the second column for each of the values of the first column. So 284, 285 and 286 values should have their consecutive standard deviation values.

284 4
284 9
284 6
285 0
285 1
285 3
286 9
286 3
286 1

I managed to calculate the running sum, but am stuck on the mean value calculation. Here is my code so far:

b = [(line.split("\t")) for line in data]
sums = [(sum(float(v) for k, v in g)) for k, g in groupby(b, key=itemgetter(0))]

lens = [(len(float(v) for k, v in g)) for k, g in groupby(b, key=itemgetter(0))]

sums works fine and calculates the summation per each change of the first column, however len() does not work and crashes with message:

TypeError: object of type 'generator' has no len()

Has anyone faced this before?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
emin
  • 53
  • 5

1 Answers1

3

The error is in this part of the code:

len(float(v) for k, v in g)

That is equivalent to:

len(g)

The generator the error is referring to is the list comprehension you are doing inside the brackets. If you actually wanted to perform the action you've written (and I don't think you do), the code would need to be:

len([float(v) for k, v in g])
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40