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 (过滤)

此操作符将根据给定的谓词函数过滤来自源 Observable 的值。

语法

filter(predicate_func)

参数

predicate_func: 此函数将决定要从源 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 的所有值,仅执行对 complete 或 error 回调函数的调用。

语法

ignore_elements()

返回值

它返回一个 Observable,它将根据源 Observable 调用 complete 或 error。

示例

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,该 Observable 将跳过输入的 count 个项目。

语法

skip(count)

参数

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,该 Observable 将跳过输入的 count 个项目(从最后开始)。

语法

skip_last(count)

参数

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_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: 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.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: 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.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
广告
© . All rights reserved.