集合、聚合与循环

阅读(1183) 标签: 集合, 聚合, 循环,

本章主要展示关于集合、聚合与循环的代码参考,比如判断是否是数列、判断是否递增数列、判断是否成员或子集、判断两个序列的成员是否相同、对有序序列用归并法快速做并交差、计算平方和方差、计算加权平均、去掉最大值和最小值的平均、取相邻行计算比上期、取相邻集合计算移动平均、汇总循环、不同数据结构的排列作并集、集合运算、TOPN问题、等频分段,用分段最大值或最小值表示该值、三个集合的笛卡尔积乘积、菲波那切数列等。

判断是否是数列

 

A

 

1

[1,2,3,4]

 

2

=A1.pselect(~!=int(~))==null

检查是否有不是整数的成员

 

判断是否递增数列

 

A

 

1

[1,2,3,4,5]

 

2

=A1.pselect(~!=int(~) || ~<=~[-1] )==null

 

 

判断是否成员或子集

 

A

 

1

[1,2,3,4,5,6,7]

 

2

=A1.pos(2)!=null

true,成员

3

=A1.pos@c([2,3])!=null

true ,连续子集

4

=A1.pos@c([3,2])!=null

false

5

=A1.pos@c([2,5])!=null

false

6

=A1.pos@i([2,3])!=null

true,子集判断

7

=A1.pos@i([3,2])!=null

false

8

=A1.pos@i([2,5])!=null

true

 

判断两个序列的成员是否相同

 

A

 

1

[1,2,3]

 

2

[3,2,1]

 

3

=A1.eq(A2)

true

 

对有序序列用归并法快速做并、交、差

 

A

 

1

=demo.query("select CLASS,STUDENTID, SUBJECT, SCORE from SCORES where CLASS=? and SUBJECT=? and STUDENTID<?","Class one", "Math",10)

 

2

=demo.query("select CLASS,STUDENTID, SUBJECT,SCORE from SCORES where CLASS=? and SUBJECT=? and STUDENTID>?","Class two", "Math",5)

 

3

=A1.sort(STUDENTID)

 

4

=A2.sort(STUDENTID)

 

5

=[A3:A4].merge(STUDENTID)

6

=[A3:A4].merge@u(STUDENTID)

7

=[A3:A4].merge@i(STUDENTID)

8

=[A3:A4].merge@d(STUDENTID)

 

计算平方和、方差

 

A

 

1

[1,2,3,4,5,6,7,8]

 

2

=A1.sum(~*~)

平方和

3

=A1.variance()

方差

4

=demo.query("select NAME,EVENT,SCORE from GYMSCORE")

 

5

=A4.variance(SCORE)

 

 

计算加权平均

 

A

 

1

[9,9.1,8.5,9.8,9.4]

 

2

[0.9,0.8,1.0,0.95,1.0]

 

3

=(A1**A2).sum()/A2.sum()

 

 

去掉最大值和最小值的平均

 

A

 

1

[99,98,95,93,87,89,90,96,94]

 

2

=(A1.sum()-A1.max()-A1.min())/(A1.len()-2)

 

3

=(A1\A1.min()\A1.max()).avg()

 

 

取相邻行计算比上期

 

A

 

1

[1,2,3,4,5,6]

 

2

=A1.(~/~[-1]-1)

 

3

=demo.query("select DATE,sum(CLOSING) AMOUNT from STOCKRECORDS GROUP BY DATE")

 

4

=A3.derive(AMOUNT/AMOUNT[-1]-1: Period-over-period)

 

 

取相邻集合计算移动平均

 

A

 

1

[1,2,3,4,5,6]

 

2

=A1.(~[-1,1].avg())

 

3

=demo.query("select STOCKID, DATE,CLOSING from STOCKRECORDS where STOCKID=?","000062")

 

4

=A3.(CLOSING[-3,3].avg())

 

 

汇总循环

 

A

 

1

[1,2,3,4,5,6,7]

 

2

=A1.iterate(~*~~;1)

返回连乘最终结果

 

不同数据结构的排列作并集

 

A

 

1

=demo.query("select * from STUDENTS")

 

2

=demo.query("select * from EMPLOYEE ").derive(interval@y(BIRTHDAY,now()):AGE)

 

3

=A1|A2

 

4

=A3.select(GENDER:"F").avg(AGE)

 

集合运算

 

A

B

 

1

[1,1,2,3,5,8]

 

 

 

2

[1,2,3,4,5,6]

 

 

3

=[A1,A2].isect()

=A1^A2

4

=[A1,A2].diff()

=A1\A2

5

=[A1,A2].union()

=A1&A2

6

=[A1,A2].conj()

=A1|A2

7

=[A1,A2].xunion()

 

保留各序列不重复成员

8

=demo.query("select * from EMPLOYEE")

 

 

9

=A8.select(GENDER:"F")

 

 

10

=A8.select(DEPT:"Sales")

 

 

11

=A8.select(age(BIRTHDAY)>=40)

 

序表同样可以进行集合运算

12

=[A9,A10,A11].isect()

=A9^A10^A11

 

13

=[A9,A10,A11].diff()

=A9\A10\A11

 

14

=[A9,A10,A11].union()

=A9&A10&A11

 

15

=[A9,A10,A11].conj()

=A9|A10|A11

 

16

=[A9,A10,A11].xunion()

 

 

 

TOPN 问题

 

A

 

1

=100.(rand(100))

 

2

=A1.top(5,~)

最小的5

3

=A1.top(-5,~)

最大的5

4

=demo.query("select * from EMPLOYEE ")

 

5

=A4.top(5,SALARY)

最小的5SALARY

6

=A4.top(-5;SALARY)

SALARY最大的5条记录

 

对位运算

 

A

 

1

[1,2,3]

 

2

[5,6,7]

 

3

=A1++A2

对位加

4

=A1--A2

对位减

5

=A1**A2

对位乘

6

=A1//A2

对位除

7

=A1%%A2

对位取余

 

等频分段,用分段最大值或最小值表示该值

 

A

 

1

=to(100).sort(rand())

 

2

=A1.median(:4)|A1.max()

确定分段

3

=A1.((n=~,A2.pseg@r(n)))

循环每个值属于哪段

4

=A3.(A2(~+1))

用各段最大值代表该值

5

=A1.min()|A1.median(:4)

确定分段

6

=A1.((n=~,A5.segp(n)))

用各段最小值代表该值

 

三个集合的笛卡尔积乘积

 

A

 

1

[1,2,3]

 

2

[2,3,4]

 

3

[3,4,5]

 

4

=A1.((x=~,A2.((y=~,A3.(x*y*~)))))

三层循环计算笛卡尔积

 

菲波那切数列

 

A

 

1

=10.iterate([~~(2),~~(1)+~~(2)],[1,1])