RxPY - 数学运算符
-
平均的
该运算符将从给定的源 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
该运算符将接收两个或多个可观察对象,并给出一个包含序列中所有值的可观察对象。句法
concat(observable1, observable2...)
参数
Observables:要连接的可观察对象列表。返回值
一个 observable 返回一个从源 observable 的值合并而来的单个值。例子
testrx.pyfrom 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
该操作符接收一个带有值的可观察对象,并将其转换为一个具有单个值的可观察对象。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
Example 2: comparer_functionfrom 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
该运算符接收一个称为累加器函数的函数,该函数用于来自源 observable 的值,它以 observable 的形式返回累加值,并将可选的种子值传递给累加器函数。句法
reduce(accumulator_func, seed=notset)
参数
accumulator_func:用于来自源 observable 的值的函数,它以 observable 的形式返回累积值。seed:可选的。未设置默认值。它是在累加器函数内部使用的初始值。返回值
它返回一个 observable,将单个值作为累加器函数的输出,应用于源 observable 的每个值。例子
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。示例 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