Python 3.7.3(default, Mar 282019,10:38:38)[MSC v.191532 bit (Intel)]
Type 'copyright','credits'or'license'for more information
IPython 7.4.0-- An enhanced Interactive Python. Type '?'forhelp.
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)...:1234567910111213# 结果验证与合并的参数顺序无关, 仅和参数内的元素值有关.
In [5]:for c in heapq.merge(b, a):...:print(c)...:1234567910111213
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*isnotNone, 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}]
chunksize =1024defreader(s):whileTrue:
data = s.recv(chunksize)if data == b'':break
process_data(data)# 上下两个方法是等效的defreader(s):for data initer(lambda: s.recv(chunksize), b''):
process_data(data)