Skip to content

Instantly share code, notes, and snippets.

@laixintao
Created June 15, 2018 02:30
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 laixintao/f4a186cea6c28fcf3dc696100458c410 to your computer and use it in GitHub Desktop.
Save laixintao/f4a186cea6c28fcf3dc696100458c410 to your computer and use it in GitHub Desktop.
In [18]: data = """
...: a, 1.324171
...: b, 0.000126
...: c, 1.970941
...: a, 1.469649
...: b, 0.000124
...: c, 0.512929
...: a, 1.290920
...: b, 0.000118
...: c, 0.259524
...: a, 0.495958
...: b, 0.000123
...: c, 0.910949
...: a, 1.268038
...: b, 0.000118
...: c, 1.016419
...: a, 1.856081
...: b, 0.000120
...: c, 1.400075
...: a, 1.314131
...: b, 0.000140
...: """
In [19]: result = {}
...: for line in data.splitlines():
...: if not line: continue
...: key, value = line.split(",")
...: result.setdefault(key, []).append(float(value))
...:
In [20]: for key, values in result.items():
...: print(f"{key}: avg: {sum(values) / len(values)}, sum: {sum(values)}")
...:
a: avg: 1.2884211428571428, sum: 9.018948
b: avg: 0.00012414285714285714, sum: 0.000869
c: avg: 1.0118061666666667, sum: 6.070837
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment