Finology 大数据金融

通过大数据以量化金融

[ 值 for 元素 in 可迭代对象 if 条件 ] # 值可以是函数表达式

[ 值1 if 条件1 else 值2 for 元素 in 可迭代对象] # 将 if-else 语句写入列表解析式

1
2
3
4
5
6
7
8
9
10
11
ret = [0.37, -1.9, 0.6, 2.54, 0.51, 1.09, 1.79, -1.2, -0.14, -0.5, -2.37, 0.0, -0.22, -0.15, 4.28, -0.85, -1.79, -0.44, -0.15, 2.34]

# 例:从收益率序列中提取出负的收益率
[i for i in ret if i < 0]

[-1.9, -1.2, -0.14, -0.5, -2.37, -0.22, -0.15, -0.85, -1.79, -0.44, -0.15]

# 正收益率赋值为1,负收益率赋值为0
[1 if i > 0 else 0 for i in ret]

[1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
1
2
3
4
ret_set = {(0.37, -1.9, 0.6, 2.54, 0.51), (1.09, 1.79, -1.2, -0.14, -0.5), (-2.37, 0.0, -0.22, -0.15, 4.28), (-0.85, -1.79, -0.44, -0.15, 2.34)}

# 如何将元素打平
[i for t in ret_set for i in t]

内循环在外循环后面

模块,以.py结尾,可以定义变量函数或者

引入模块

1
import module1 [, module2 [, ...moduleN]]
1
import  module1[,module2[,....moduleN]] as ...  # 对模块重新命名,一般用于简化模块名

调用模块

通过import语句引入后调用

调用函数,必须通过模块名.函数名的形式来调用。

1
2
3
4
5
import time

time.tzname

('CST', 'CST')

通过from…import语句只导入一个指定的部分

1
from module1 import [,funName1 [, ...funNameN]]
1
2
3
4
5
from time import tzname

tzname

('CST', 'CST')

通过from…import *语句导入所有内容

这时调用函数不需要使用模块名.函数名的方式来调用。

1
2
3
4
5
from time import *

tzname

('CST', 'CST')

Mybatis中的一对多关系,用collection。

一个学生表,Student,一个老师表,Teacher。一个老师下面有多个学生。学生表有一个字段 teacher_id。

学生表对应的类

1
2
3
4
5
6
7
8
9
10
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student {

private Integer id;
private String name;
private Integer teacherId;
}

教师类

1
2
3
4
5
6
7
8
9
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {

private Integer id;
private Integer age;
}

我们要查出教师及其学生的列表。

教师及学生列表类

1
2
3
4
5
6
7
8
9
10
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TeacherInfo {

private Integer id;
private String name;
private List<Student> students;
}

获取所以教师及学生列表信息的 DAO 层接口。

1
2
3
4
public interface TeacherDao {

List<TeacherInfo> findTeachers();
}

上述接口对应的 xml 文件的核心内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="gy.finolo.dao.TeacherDao">

<resultMap id="teacherInfoMap" type="gy.finolo.model.TeacherInfo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="age" jdbcType="INTEGER" property="age"/>
<collection property="students" ofType="gy.finolo.model.Student">
<!-- <id column="student_id" property="id" /> -->
<result column="name" property="name" />
</collection>
</resultMap>

<select id="findTeachers" resultMap="teacherInfoMap">
SELECT t.id, t.age, s.name FROM t_teacher t INNER JOIN t_student s ON (t.id = s.teacher_id);
</select>

</mapper>

需要说明的有以下几点:

  1. 主表和副表的 id 标签都可以不要,这时只是属性里面没有 id 的值而已。我之前以为是以 id 来判断两条记录是否相同的,现在看来并不是。

  2. 如果需要获取学生id值,为了避免column字段重复,所以在SQL语句写别名,同时在xml也做相应修改。

1
<id column="student_id" property="id" />
1
2
3
<select id="findTeachers" resultMap="teacherInfoMap">
SELECT t.id, t.age, s.id student_id, s.name FROM t_teacher t INNER JOIN t_student s ON (t.id = s.teacher_id);
</select>
0%