Python:orator/backpack内置数据操作类Collection
发布日期:2021-07-01 06:08:33 浏览次数:2 分类:技术文章

本文共 3714 字,大约阅读时间需要 12 分钟。

文档

支持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 reducereject reverse serialize shift sort sum take to_json transform unique where zip

安装

pip install backpack

导入模块

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

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] = 5print(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))# Trueprint(1 in Collection([1, 2, 3]))# Trueprint(Collection([1, 2, 3]).contains(lambda item: item > 1))# Trueprint(Collection(data).contains('name', 'Simon'))# False

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'}

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)]

5、计算操作

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

6、序列化

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

转载地址:https://pengshiyu.blog.csdn.net/article/details/103519009 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Python:环境变量设置PYTHONPATH解决impor导入模块报错
下一篇:Python:pendulum库处理时间

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年05月05日 03时17分13秒