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.