欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python define function的顺序_Python怎么根据一个函数来决定列表顺序

发布时间:2023/12/2 python 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python define function的顺序_Python怎么根据一个函数来决定列表顺序 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

def__init__(self,function):self._queue=[]self.function=function然后要写一往列表里增加元素的方法,按照function排序,function类型为函数defadd(self,item):"""AddtothisP...

def __init__(self,function):

self._queue = []

self.function = function

然后要写一往列表里增加元素的方法,按照function排序,function类型为函数

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']

"""

就想达到这种效果

函数不一定要上面举得例子 shorter(a,b) ,function应该只有两个函数,只返回布尔类型。

展开

总结

以上是生活随笔为你收集整理的python define function的顺序_Python怎么根据一个函数来决定列表顺序的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。