简介

官方文档
Pipe 是一个 Python 库,可让你在 Python 中使用管道。 管道 | 将一种方法的结果传递给另一种方法。

安装

pip install pipe

用法

where

用于可迭代对象中的过滤元素
举例

from pipe import where

arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr1 = list(arr | where(lambda x: x % 2 == 0))
arr2 = sum(arr | where(lambda x: x % 2 != 0))
print(arr1)
print(arr2)

#结果输出
[2, 4, 6, 8]
16

Process finished with exit code 0

map(),select()

将作为参数给定的转换表达式应用于给定可迭代的每个元素,即将方法应用于可迭代的每个元素。

from pipe import map, select

arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr1 = list(arr | map(lambda x: x * x))
arr2 = list(arr | select(lambda x: x * x))
print(arr1)
print(arr2)

#结果输出
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64]

Process finished with exit code 0

将可迭代对象展开

对于嵌套结构的iterable数据,最难任务之一就是将其展平。

chain

from pipe import chain

arr = [[1, 2], [3, 4], [5]]
arr1 = list(arr | chain)
print(arr1)

#结果输出
[1, 2, 3, 4, 5]

Process finished with exit code 0

注意:chain只能展开可迭代,仅包含可迭代

from pipe import chain

arr = [1, 2, [3]]
arr1 = list(arr | chain)
print(arr1)

#结果输出
Traceback (most recent call last):
  File "D:/红盒子/校验/pipe教程.py", line 4, in <module>
    arr1 = list(arr | chain)
TypeError: 'int' object is not iterable

Process finished with exit code 1

traverse

递归展开可迭代对象

from pipe import traverse, select

arr = [[1, 2, [3]], [4, 5]]
arr1 = [[1, 3], 2, 4]
arr2 = list(arr | traverse)
arr3 = list(arr1 | traverse | select(lambda x: x * x))
print(arr2)
print(arr3)

#结果输出
[1, 2, 3, 4, 5]
[1, 9, 4, 16]

Process finished with exit code 0

groupby 分组

使用特定函数对列表中的元素进行分组

# -*- coding: utf-8 -*-
from pipe import select, groupby

arr = range(10)

arr2 = list(arr
            | groupby(lambda x: "偶数" if x % 2 == 0 else "奇数")
            )
arr3 = list(arr
            | groupby(lambda x: "偶数" if x % 2 == 0 else "奇数")
            | select(lambda x: {x[0]: list(x[1])})
            )
print(arr2)
print(arr3)

#结果输出
('偶数', <itertools._grouper object at 0x0000025DE5DAF7C8>), ('奇数', <itertools._grouper object at 0x0000025DE5DC0408>)]
[{'偶数': [0, 2, 4, 6, 8]}, {'奇数': [1, 3, 5, 7, 9]}]

Process finished with exit code 0

在上面的代码中, 我们首先使用groupby将数字分为奇数组和偶数组。groupby方法输出的结果如下

('偶数', <itertools._grouper object at 0x0000025DE5DAF7C8>),
('奇数', <itertools._grouper object at 0x0000025DE5DC0408>)]

接下来,使用select将元素为元组的列表转化为字典,其中
元组中第1位置做字典的关键词
元组中第2位置做字典的值

dedup 方法删除列表中的重复

使用给定的函数(如果提供)删除重复值。key

# -*- coding: utf-8 -*-
from pipe import dedup

arr = [1, -2, 2, 3, 4, 5, 6, 6, 7, 9, 3, 3, -1]
arr1 = list(arr | dedup)
arr2 = list(arr | dedup(lambda key: key < 5))
arr3 = list(arr | dedup(lambda key: abs(key)))
print(arr1)
print(arr2)
print(arr3)

#结果输出
[1, -2, 2, 3, 4, 5, 6, 7, 9, -1]
[1, 5]
[1, -2, 3, 4, 5, 6, 7, 9]

Process finished with exit code 0

permutations返回所有可能的排列:

permutations(r=None)

# -*- coding: utf-8 -*-
from pipe import permutations

for item in 'ABC' | permutations(3):
    print(item)
    
#结果输出
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')

Process finished with exit code 0

skip()

跳过给定可迭代的给定元素量,然后产生

# -*- coding: utf-8 -*-
from pipe import skip

print(list((1, 2, 3, 4, 5) | skip(3)))

#结果输出
[4, 5]

Process finished with exit code 0

skip_while(predicate)

在谓词为真时跳过给定可迭代的元素,然后生成其他元素:

# -*- coding: utf-8 -*-
from pipe import skip_while

print(list([1, 2, 5, 1, 7] | skip_while(lambda x: x < 3)))

#结果输出
[5, 1, 7]

Process finished with exit code 0

transpose()

转置矩阵的行和列。

# -*- coding: utf-8 -*-
from pipe import transpose

print(list([[1, 2, 3], [4, 5, 6], [7, 8, 9]] | transpose()))

#结果输出
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Process finished with exit code 0