Takes the average of elements in the sequence >>> seq([1, 2]).average() 1.5 >>> seq([('a', 1), ('b', 2)]).average(lambda x: x[1]) :param projection: function to project on the sequence before taking the average :return: average of elements in the s
(self, projection=None)
| 1023 | return sum(self) |
| 1024 | |
| 1025 | def average(self, projection=None): |
| 1026 | """ |
| 1027 | Takes the average of elements in the sequence |
| 1028 | |
| 1029 | >>> seq([1, 2]).average() |
| 1030 | 1.5 |
| 1031 | |
| 1032 | >>> seq([('a', 1), ('b', 2)]).average(lambda x: x[1]) |
| 1033 | |
| 1034 | :param projection: function to project on the sequence before taking the average |
| 1035 | :return: average of elements in the sequence |
| 1036 | """ |
| 1037 | length = self.size() |
| 1038 | if projection: |
| 1039 | return sum(self.map(projection)) / length |
| 1040 | else: |
| 1041 | return sum(self) / length |
| 1042 | |
| 1043 | def aggregate(self, *args): |
| 1044 | """ |