Average

See also

  • Official ReactiveX documentation: Average
Observable.average(key_selector=None)

Computes the average of an observable sequence of values that are in the sequence or obtained by invoking a transform function on each element of the input sequence if present.

Example res = source.average(); res = source.average(lambda x: x.value)

Parameters:
  • self (Observable) – Observable to average.
  • key_selector (types.FunctionType) – A transform function to apply to each element.
Returns:

An observable sequence containing a single element with the average of the sequence of values.

Return type:

Observable

../../_images/average.c.png
>>> import rx
>>> source = rx.Observable.range(0, 9).average()
>>> subscription = source.subscribe(
...     lambda value: print("Next:", value),
...     lambda error: print("Error:", error),
...     lambda: print("Complete!")
Next: 4.0
Complete!
../../_images/average.f.png
>>> import rx
>>> shapes = [
...     {'name': 'triangle', 'sides': 3},
...     {'name': 'square', 'sides': 4},
...     {'name': 'pentagon', 'sides': 5},
...     {'name': 'hexagon', 'sides': 6},
...     {'name': 'heptagon', 'sides': 7},
...     {'name': 'octogon', 'sides': 8},
...   ]
>>> source = rx.Observable.from_(shapes).average(lambda shape: shape['sides'])
>>> subscription = source.subscribe(
...     lambda value: print("Next:", value),
...     lambda error: print("Error:", error),
...     lambda: print("Complete!")
Next: 5.5
Complete!