如何在Python中使用反应式扩展(Rx)LINQ?

2024-05-29 02:33:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我下载了RxPY并正在观看Rx tutorials时遇到了:

enter image description here

那么,如何使用RxPY在Python中实现上述功能呢?特别是,查询q = from x in xs where...在语法上不是有效的Python代码——那么这将如何改变呢?在


Tags: 代码infrom功能语法whererxrxpy
2条回答

C中的所有LINQ查询都可以很容易地转换为扩展方法(^{}表示^{},而{a3}表示^{}):

In [20]: from rx import Observable

In [21]: xs = Observable.range(1, 10)

In [22]: q = xs.where(lambda x: x % 2 == 0).select(lambda x: -x)

In [23]: q.subscribe(print)
-2
-4
-6
-8
-10

您也可以使用filter代替where和{}而不是{}:

^{pr2}$

您所展示的图片是用C#编写的代码,而不是Python。 在Python中如下所示:

from rx import Observable
xs = Observable.range(1, 10)
q = xs.filter(lambda x: x % 2 == 0).map(lambda x: -x)
q.subscribe(print)

一般文档可以在文档部分的http://reactivex.io找到。 特定于Python的文档位于https://github.com/ReactiveX/RxPY/blob/master/README.md。在

相关问题 更多 >

    热门问题