RxPY - 过滤运算符
-
debounce
该运算符将给出源 observable 的值,直到给定的时间跨度,如果时间过去,则忽略其余的值。句法
debounce(duetime)
参数
Duetime:这将以秒或时间实例为单位,持续时间将决定从源 observable 返回的值。例子
from rx import of, operators as op from datetime import date test = of(1,2,3,4,5,6,7,8,9,10) sub1 = test.pipe( op.debounce(2.0) ) sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py The value is 10
-
distinct
该运算符将给出与源 observable 不同的所有值。句法
distinct()
返回值
它将返回一个 observable,其中它将具有与源 observable 不同的值。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.distinct() ) sub1.subscribe(lambda x: print("The distinct value is {0}".format(x)))
输出
E:\pyrx>python testrx.py The distinct value is 1 The distinct value is 6 The distinct value is 15 The distinct value is 10 The distinct value is 40 The distinct value is 58 The distinct value is 20
-
element_at
该运算符将为给定的索引从源 observable 中给出一个元素。句法
element_at(index)
参数
index:从零开始的数字,您需要源 observable 中的元素。返回值
它将返回一个带有给定索引的源 observable 值的 observable。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.element_at(5) ) sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py The value is 6
-
filter
该运算符将根据给定的谓词函数过滤来自可观察源的值。句法
filter(predicate_func)
参数
predicate_func:这个函数将决定从源 observable 中过滤的值。返回值
它将返回一个 observable,该 observable 将具有来自基于谓词函数的源 observable 的过滤值。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.filter(lambda x : x %2==0) ) sub1.subscribe(lambda x: print("The filtered value is {0}".format(x)))
在示例中,我们过滤了所有偶数。输出
E:\pyrx>python testrx.py The filtered value is 6 The filtered value is 10 The filtered value is 6 The filtered value is 40 The filtered value is 10 The filtered value is 58 The filtered value is 20 The filtered value is 40
-
first
该运算符将给出源 observable 中的第一个元素。句法
first(predicate_func=None)
参数
predicate_func:(可选)如果通过,此函数将根据条件决定要选择的第一个元素。返回值
它将返回一个带有源 observable 的第一个值的 observable。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.first() ) sub1.subscribe(lambda x: print("The first element is {0}".format(x)))
输出
E:\pyrx>python testrx.py The first element is 1
示例 2:使用 predicate_func
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.first(lambda x : x%2==0) ) sub1.subscribe(lambda x: print("The first element is {0}".format(x)))
输出
E:\pyrx>python test1.py The first element is 6
-
ignore_elements
该运算符将忽略源 Observable 中的所有值,并且只执行对完成或错误回调函数的调用。句法
ignore_elements()
返回值
它返回一个 observable,它将根据源 observable 调用完成或错误。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.ignore_elements() ) sub1.subscribe(lambda x: print("The first element is {0}".format(x)), lambda e: print("Error : {0}".format(e)), lambda: print("Job Done!"))
输出
E:\pyrx>python testrx.py Job Done!
-
last
该运算符将给出源 observable 中的最后一个元素。句法
last(predicate_func=None)
参数
predicate_func:(可选)如果通过,此函数将根据条件决定要选择的最后一个元素。返回值
它将返回一个带有源 observable 的最后一个值的 observable。例子
from rx import of, operators as op from datetime import date test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40) sub1 = test.pipe( op.last() ) sub1.subscribe(lambda x: print("The last element is {0}".format(x)))
输出
E:\pyrx>python test1.py The last element is 40
-
skip
该运算符将返回一个 observable,它将跳过作为输入的计数项的第一次出现。句法
skip(count)
参数
count:计数是从源 observable 中跳过项目的次数。返回值
它将返回一个 observable,根据给定的计数跳过值。例子
from rx import of, operators as op from datetime import date test = of(1, 2,3,4,5,6,7,8,9,10) sub1 = test.pipe( op.skip(5) ) sub1.subscribe(lambda x: print("The element is {0}".format(x)))
输出
E:\pyrx>python testrx.py The element is 6 The element is 7 The element is 8 The element is 9 The element is 10
-
skip_last
该运算符将返回一个 observable,它将跳过最后一次出现的计数项作为输入。句法
skip_last(count)
参数
count:计数是从源 observable 中跳过项目的次数。返回值
它将返回一个 observable,它会根据 last 给出的计数跳过值。例子
from rx import of, operators as op from datetime import date test = of(1, 2,3,4,5,6,7,8,9,10) sub1 = test.pipe( op.skip_last(5) ) sub1.subscribe(lambda x: print("The element is {0}".format(x)))
输出
E:\pyrx>python testrx.py The element is 1 The element is 2 The element is 3 The element is 4 The element is 5
-
take
该运算符将根据给定的计数以连续顺序给出源值列表。句法
take(count)
参数
count:计数是项目的数量,将从可观察的源中给出。返回值
它将根据给定的计数返回一个具有连续顺序的值的 observable。例子
from rx import of, operators as op from datetime import date test = of(1, 2,3,4,5,6,7,8,9,10) sub1 = test.pipe( op.take(5) ) sub1.subscribe(lambda x: print("The element is {0}".format(x)))
输出
E:\pyrx>python testrx.py The element is 1 The element is 2 The element is 3 The element is 4 The element is 5
-
take_last
该运算符将根据给定的计数以从最后一个连续的顺序给出源值列表。句法
take_last(count)
参数
count:计数是项目的数量,将从可观察的源中给出。返回值
它将返回一个 observable,它的值根据给定的计数从最后一个连续顺序排列。例子
from rx import of, operators as op from datetime import date test = of(1, 2,3,4,5,6,7,8,9,10) sub1 = test.pipe( op.take_last(5) ) sub1.subscribe(lambda x: print("The element is {0}".format(x)))
输出
E:\pyrx>python testrx.py The element is 6 The element is 7 The element is 8 The element is 9 The element is 10