Pythonorator/backpack内置数据操作类Collection

网友投稿 509 2022-05-30

文档

https://orator-orm.com/docs/0.9/collections.html

支持36个函数

all avg chunk collapse contains count diff each every filter first flatten forget for_page get implode is_empty last map merge pluck pop prepend pull push put reduce reject reverse serialize shift sort sum take to_json transform unique where zip

1

2

3

4

5

安装

pip install backpack

1

导入模块

# 如果安装了 orator可以使用 from orator import Collection # 或者 from backpack import Collection # 测试使用的data数据 data = [ {'name': 'Tom', 'age': 23}, {'name': 'Jack', 'age': 25} ]

1

2

3

4

5

6

7

8

9

10

11

1、简单的CURD

# 所有数据 print(Collection([1, 2, 3]).all()) [1, 2, 3] # 取第一个值 print(Collection([1, 2, 3]).first()) 1 # 带条件取值 print(Collection([1, 2, 3]).first(lambda item: item > 1)) 2 # 取最后一个值 print(Collection([1, 2, 3]).last()) 3 # 分页取值 print(Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]).for_page(2, 3).all()) [4, 5, 6] # 间隔取数 print(Collection([1, 2, 3]).every(2).all()) [1, 3] # 前往后取值 print(Collection([2, 1, 3]).take(2).all()) [2, 1] # 后往前取值 print(Collection([2, 1, 3]).take(-1).all()) [3] # 切片取值 print(Collection([1, 2, 3, 4, 5, 6])[1:4:2].all()) # [2, 4] # 获取值 print(Collection([1, 2, 3]).get(4, 'default')) # default # 根据键设置值 print(Collection([1, 2, 3]).put(0, 5).all()) [5, 2, 3] c = Collection([1, 2, 3]) c[0] = 5 print(c.all()) [5, 2, 3] # 移除数据,不返回值 print(Collection([1, 2, 3]).forget(1).all()) [1, 3] # 返回移除 print(Collection([1, 2, 3]).pull(0)) 1 # 前部插入 print(Collection([1, 2, 3]).prepend(0).all()) [0, 1, 2, 3] # 弹出第一个值 print(Collection([1, 2, 3]).shift()) 1 # 尾部插入 print(Collection([1, 2, 3]).push(4).all()) [1, 2, 3, 4] print(Collection([1, 2, 3]).append(4).all()) [1, 2, 3, 4] # 弹出 print(Collection([1, 2, 3]).pop(1)) # 条件取值 print(Collection(data).where('age', 23).all()) [{'name': 'Tom', 'age': 23}] 2、判断操作 ```python # 空值测试 print(Collection([]).is_empty()) True # 包含 print(Collection([1, 2, 3]).contains(1)) # True print(1 in Collection([1, 2, 3])) # True print(Collection([1, 2, 3]).contains(lambda item: item > 1)) # True print(Collection(data).contains('name', 'Simon')) # False

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

Python:orator/backpack内置数据操作类Collection

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

3、数据变换

# 反转 print(Collection([1, 2, 3]).reverse().all()) [3, 2, 1] # 排序 print(Collection([2, 1, 3]).sort().all()) [1, 2, 3] # 取唯一值 print(Collection([2, 1, 3, 3]).unique().all()) [2, 1, 3] # 变换数据,修改自身 print(Collection([2, 1, 3]).transform(lambda item: item * 2).all()) [4, 2, 6] # 仅迭代,不修改原对象 print(Collection([1, 2, 3]).each(lambda x: x + 1).all()) [1, 2, 3] # 过滤 print(Collection([1, 2, 3]).filter(lambda item: item > 2).all()) [3] # 映射 print(Collection([1, 2, 3]).map(lambda x: x + 1).all()) [2, 3, 4] # 移除满足条件的值 print(Collection([1, 2, 3, 4]).reject(lambda item: item > 3).all()) [1, 2, 3] # 拆分 print(Collection([1, 2, 3]).chunk(size=2).serialize()) [[1, 2], [3]] # 塌陷 print(Collection([[1, 2], [3, 4]]).collapse().all()) [1, 2, 3, 4] # 压平数据,保留值 print(Collection([1, 2, [3, 4, 5, {'foo': 'bar'}]]).flatten().all()) [1, 2, 3, 4, 5, 'bar'] # 取字典值 print(Collection(data).pluck('name').all()) ['Tom', 'Jack'] print(Collection(data).pluck('name', 'age')) {23: 'Tom', 25: 'Jack'}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

4、两个集合操作

# 差异比较 print(Collection([1, 2, 3]).diff([2, 3, 4]).all()) [1] # 合并 print(Collection([1, 2, 3]).merge([1, 2, 3]).all()) [1, 2, 3, 1, 2, 3] # 合并序列 print(Collection([1, 2, 3]).zip([4, 5, 6]).all()) [(1, 4), (2, 5), (3, 6)]

1

2

3

4

5

6

7

8

9

10

11

5、计算操作

# 计数 print(Collection([1, 2, 3]).count()) 3 # 计数 print(len(Collection([1, 2, 3]))) 3 # 平均数 print(Collection([1, 2, 3]).avg()) 2.0 print(Collection(data).avg('age')) 24.0 # 求和 print(Collection([2, 1, 3]).sum()) 6 print(Collection(data).sum('age')) 48 # 累积计算 print(Collection([1, 2, 3]).reduce(lambda result, item: result + item, 0)) 6

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

6、序列化

# 取值拼接 print(Collection(data).implode('name', ', ')) # Tom, Jack print(Collection(['foo', 'bar', 'baz']).implode('-')) # foo-bar-baz # 转字符串 print(Collection(data).serialize()) [{'name': 'Tom', 'age': 23}, {'name': 'Jack', 'age': 25}] # 转json print(Collection(data).to_json()) [{"name": "Tom", "age": 23}, {"name": "Jack", "age": 25}]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:学习笔记:发布自己的python模块安装包
下一篇:使用云连接CC和数据复制服务DRS实现跨区域RDS迁移和数据同步
相关文章