RxJs RxPy都是一家,今天换个口味
目标是统计列表中数字的频率
import numpy as np
from rx import Observable
p = np.random.randint(0, 26, 1000)
x = Observable.from_(p)
x.group_by(lambda s: s) \
.map( lambda grp: grp.count().map(lambda ct: "{0:02} -> {1:02}".format(grp.key,ct))) \
.merge_all() \
.subscribe(print)
思路是先分组,然后组内计数,并格式化,最后归并打印
将Observable转换成列表的方法
list(x.group_by(lambda s: s) \
.map( lambda grp: grp.count().map(lambda ct: "{0:02} -> {1:02}".format(grp.key,ct))) \
.merge_all().to_blocking())
统计01个数,首先转换成bin值,
import numpy as np
from rx import Observable
x = Observable.from_(np.random.randint(0, 26, 1000))
a = list(x.map(lambda v: "{0:05b}".format(v)) \
.reduce(lambda acc, curr : acc + curr,'').to_blocking())
Observable.from_iterable(a[0]) \
.group_by(lambda s: s) \
.map( lambda grp: grp.count()) \
.merge_all() \
.subscribe(print)
网友评论