RxPY - 數學運算子
平均值 (average)
此運算子將計算給定源 Observable 的平均值,並輸出一個包含平均值的 Observable。
語法
average()
返回值
它返回一個包含平均值的 Observable。
示例
from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
op.average()
)
sub1.subscribe(lambda x: print("Average is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Average is 5.5
連線 (concat)
此運算子將接收兩個或多個 Observable,並返回一個包含所有值的單個 Observable。
語法
concat(observable1, observable2...)
引數
Observables:要連線的 Observable 列表。
返回值
返回一個 Observable,其中包含從源 Observable 值合併的單個值。
示例
testrx.py
from rx import of, operators as op
test = of(2, 4, 6, 8, 10)
test2 = of(3,6,9,12,15)
sub1 = test.pipe(
op.concat(test2)
)
sub1.subscribe(lambda x: print("Final value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Final value is 2 Final value is 4 Final value is 6 Final value is 8 Final value is 10 Final value is 3 Final value is 6 Final value is 9 Final value is 12 Final value is 15
計數 (count)
此運算子接收一個包含值的 Observable,並將其轉換為一個包含單個值的 Observable。count 函式可選擇接受一個謂詞函式作為引數。該函式的型別為布林值,只有在滿足條件時,才會將值新增到輸出。
語法
count(predicate_function=None)
引數
count 函式可選擇接受一個謂詞函式作為引數。該函式的型別為布林值,只有在滿足條件時,才會將值新增到輸出。
返回值
它將返回一個包含單個值的 Observable,即源 Observable 的計數。
示例 1
from rx import of, operators as op
test = of(1,2,3, 4,5, 6,7, 8,9, 10)
sub1 = test.pipe(
op.count()
)
sub1.subscribe(lambda x: print("The count is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The count is 10
示例 2:使用謂詞函式
from rx import of, operators as op
test = of(1,2,3, 4,5, 6,7, 8,9, 10)
sub1 = test.pipe(
op.count(lambda x : x %2 == 0)
)
sub1.subscribe(lambda x: print("The count of even numbers is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The count of even numbers is 5
最大值 (max)
此運算子將返回一個包含源 Observable 中最大值的 Observable。
語法
max(comparer_function=None)
引數
comparer_function:可選引數。此函式用於比較源 Observable 中的值。
返回值
它返回一個包含源 Observable 中最大值的 Observable。
示例 1
from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
op.max()
)
sub1.subscribe(lambda x: print("Max value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Max value is 280
示例 2:comparer_function
from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
op.max(lambda a, b : a - b)
)
sub1.subscribe(lambda x: print("Max value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Max value is 280
最小值 (min)
此運算子將返回一個包含源 Observable 中最小值的 Observable。
語法
min(comparer_function=None)
引數
comparer_function:可選引數。此函式用於比較源 Observable 中的值。
返回值
它返回一個包含源 Observable 中最小值的 Observable。
示例 1
from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
op.min()
)
sub1.subscribe(lambda x: print("Min value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Min value is 12
示例 2:使用 comparer_function
from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
op.min(lambda a, b : a - b)
)
sub1.subscribe(lambda x: print("Min value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py Min value is 12
規約 (reduce)
此運算子接收一個名為 accumulator 函式的函式,該函式用於處理來自源 Observable 的值,並以 Observable 的形式返回累加值,並可以選擇向 accumulator 函式傳遞種子值。語法
reduce(accumulator_func, seed=notset)
引數
accumulator_func:用於處理來自源 Observable 的值的函式,它以 Observable 的形式返回累加值。
seed:可選。預設值未設定。它是 accumulator 函式中使用的初始值。
返回值
它返回一個 Observable,其輸出值為應用於源 Observable 的每個值的 accumulator 函式的單個值。
示例
from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
op.reduce(lambda acc, x: acc + x)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The value is 55
求和 (sum)
此運算子將返回一個包含來自源 Observable 所有值之和的 Observable。
語法
sum(key_mapper=none)
引數
key_mapper:可選。這是應用於來自源 Observable 的值的函式。
返回值
它返回一個包含來自源 Observable 所有值之和的 Observable。
示例 1
from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
op.sum()
)
sub1.subscribe(lambda x: print("The sum is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The sum is 55
示例 2:使用 key_mapper 函式
from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
op.sum(lambda a: a+1)
)
sub1.subscribe(lambda x: print("The sum is {0}".format(x)))
使用 key_mapper 函式,我們將所有值加 1 並求和。
E:\pyrx>python testrx.py The sum is 65