Skip to content

Instantly share code, notes, and snippets.

@greggy
Created November 29, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggy/4170375 to your computer and use it in GitHub Desktop.
Save greggy/4170375 to your computer and use it in GitHub Desktop.
def stdDevOfLengths(L):
"""
L: a list of strings
returns: float, the standard deviation of the lengths of the strings,
or NaN if L is empty.
"""
if L:
mean = float(sum([len(s) for s in L]))/float(len(L))
stdDiv = 0.0
for s in L:
stdDiv += (len(s) - mean)**2
return (stdDiv / len(L))**0.5
else:
return float('NaN')
def stdDevOfLengths(L):
"""
L: a list of strings
returns: float, the standard deviation of the lengths of the strings,
or NaN if L is empty.
"""
if L:
mean = float(sum([len(s) for s in L]))/float(len(L))
stdDiv = 0.0
for s in L:
stdDiv += (len(s) - mean)**2
return (stdDiv / len(L))**0.5
else:
return float('NaN')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment