Python:orator/backpack內置數據操作類Collection
文檔
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
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小時內刪除侵權內容。