python heapq及iter 使用样例

一. heapq 使用场景

  • 合并多个有序序列

1.1 代码示例

Python 3.7.3 (default, Mar 28 2019, 10:38:38) [MSC v.1915 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import heapq

In [2]: a = [1, 2, 4, 5, 10, 11]

In [3]: b = [3, 6, 7, 9, 12, 13]

In [4]: for c in heapq.merge(a, b):
   ...:     print(c)
   ...:
1
2
3
4
5
6
7
9
10
11
12
13
# 结果验证与合并的参数顺序无关, 仅和参数内的元素值有关.
In [5]: for c in heapq.merge(b, a):
   ...:     print(c)
   ...:
1
2
3
4
5
6
7
9
10
11
12
13
In [6]: help(heapq.merge)
Help on function merge in module heapq:

merge(*iterables, key=None, reverse=False)
    Merge multiple sorted inputs into a single sorted output.

    Similar to sorted(itertools.chain(*iterables)) but returns a generator,
    does not pull the data into memory all at once, and assumes that each of
    the input streams is already sorted (smallest to largest).

    >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
    [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]

    If *key* is not None, applies a key function to each element to determine
    its sort order.

    >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len))
    ['dog', 'cat', 'fish', 'horse', 'kangaroo']

# 取两个队列中最小的三个元素
In [7]: heapq.nsmallest(3, heapq.merge(a, b))
Out[7]: [1, 2, 3]
# 取两个队列中最大的三个元素
In [8]: heapq.nlargest(3, heapq.merge(a, b))
Out[8]: [13, 12, 11]

In [9]: c = [{'name': 'IBM', 'shares': 100, 'price': 91.1},
    ...: {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    ...: {'name': 'FB', 'shares': 200, 'price': 21.09},
    ...: {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    ...: {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    ...: {'name': 'ACME', 'shares': 75, 'price': 115.65}
    ...: ]
# 取价格最小的三个元素
In [10]: heapq.nsmallest(3, c, key=lambda x: x['price'])
Out[10]:
[{'name': 'YHOO', 'shares': 45, 'price': 16.35},
 {'name': 'FB', 'shares': 200, 'price': 21.09},
 {'name': 'HPQ', 'shares': 35, 'price': 31.75}]

  • heapq.merge(*iterables, key=None, reverse=False)函数

二. iter()的使用场景

  • 取代while True
  • 这是个内建函数, 它可以选择性接受一个无参的可调用对象及一个结束值作为输入, 会返回一个迭代器

2.1 代码示例

chunksize = 1024
def reader(s):
    while True:
        data = s.recv(chunksize)
        if data == b'':
            break
        process_data(data)

# 上下两个方法是等效的

def reader(s):
    for data in iter(lambda: s.recv(chunksize), b''):
        process_data(data)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值