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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章