python define function的顺序_Python怎么根据一个函数来决定列表顺序
def__init__(self,function):self._queue=[]self.function=function然后要寫一往列表里增加元素的方法,按照function排序,function類型為函數(shù)defadd(self,item):"""AddtothisP...
def __init__(self,function):
self._queue = []
self.function = function
然后要寫一往列表里增加元素的方法,按照function排序,function類型為函數(shù)
def add(self, item):
"""Add to this PriorityQueue.
@type self: PriorityQueue
@type item: Object
@rtype: None
>>> def shorter(a, b):
... return len(a) < len(b)
...
>>>
>>> # Define a PriorityQueue with priority on shorter strings.
>>> # I.e., when we remove, we get the shortest remaining string.
>>> pq = PriorityQueue(shorter)
>>> pq.add('fred')
>>> pq.add('arju')
>>> pq.add('monalisa')
>>> pq.add('hat')
>>> pq._queue
['monalisa', 'arju', 'fred', 'hat']
>>> pq.remove()
'hat'
>>> pq._queue
['monalisa', 'arju', 'fred']
"""
就想達(dá)到這種效果
函數(shù)不一定要上面舉得例子 shorter(a,b) ,function應(yīng)該只有兩個(gè)函數(shù),只返回布爾類型。
展開
總結(jié)
以上是生活随笔為你收集整理的python define function的顺序_Python怎么根据一个函数来决定列表顺序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python写九九乘法口诀表左上角_p
- 下一篇: wxpython图形编程_wxpytho