Python 按cdef类中的属性对cdef对象列表进行排序
我想对这个类的列表Python 按cdef类中的属性对cdef对象列表进行排序,python,sorting,cython,Python,Sorting,Cython,我想对这个类的列表内部进行排序。该列表包含名为Edge的cdef类的对象。Edge类包含一个名为savings的成员变量。我想按此变量对列表进行排序 cdef class Alist: def __init__(self): self.inner = [] cdef list inner cdef void append(self, Edge a): self.inner.append(a) cdef void pop(self,
内部进行排序。该列表包含名为Edge
的cdef类的对象。Edge类包含一个名为savings
的成员变量。我想按此变量对列表进行排序
cdef class Alist:
def __init__(self):
self.inner = []
cdef list inner
cdef void append(self, Edge a):
self.inner.append(a)
cdef void pop(self, int a):
self.inner.pop(a)
cdef void insert(self,int pos,Edge a):
self.inner.insert(pos,a)
cdef int index(self,Edge a):
if a in self.inner:
return self.inner.index(a)
return -1
cdef Edge get(self, int i):
return <Edge> self.inner[i]
cdef void sort(self):
self.inner.sort(key = lambda c : c.Savings)
#self.inner.sort()
def __len__(self):
return len(self.inner)
def __richcmp__(Edge self, Edge other,int op):
if op == 0:
if self.inner.savings < other.inner.savings:
return True
return False
cdef类列表:
定义初始化(自):
self.inner=[]
cdef列表内部
cdef void append(自身,边缘a):
self.inner.append(a)
cdef void pop(自身,内部a):
self.inner.pop(a)
cdef无效插入(自身、内部位置、边缘a):
自内插入件(位置a)
cdef int索引(自身,边a):
如果在self.inner中有一个:
返回自内索引(a)
返回-1
cdef边缘获取(自身,内部i):
返回自我内部[i]
cdef无效排序(自):
self.internal.sort(key=lambda c:c.Savings)
#self.inner.sort()
定义(自我):
返回透镜(自内)
定义(边缘自身、边缘其他、内部操作):
如果op==0:
如果self.inner.savings
为此,我在类中创建了方法sort
,但是当我执行它时,我得到了以下错误消息:
异常AttributeError:“fib.Alist.sort”中的“fib.Edge”对象没有属性“savings”
这里出了什么问题
Python-lambda函数无法访问Edge
的cdef属性。如果试图从python直接访问cdef类的属性,则会出现错误,除非将只读
(用于读取访问)或公共
(用于读取和写入访问)添加到属性定义中
以此边缘类为例:
cdef class Edge:
cdef readonly int savings # visible from python
cdef int foo # not visible from python
def __init__(self, int s):
self.savings = s
self.foo = 42
def __repr__(self):
"""Friendly representation so we can see how the edges sort."""
return "Edge: {}".format(self.savings)
如果我们编译它(假设它位于文件sortlist.pyx
)并将其导入Python shell中:
In [1]: import sortlist as sl
In [2]: e = sl.Edge(42)
In [3]: e.savings
Out[3]: 23
In [4]: e.foo
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-3ebfe6d526e9> in <module>()
----> 1 e.foo
AttributeError: 'sortlist.Edge' object has no attribute 'foo'
为什么您可能不需要richcmp(现在的位置)
当您在Alist
中定义\uuu\richcmp\uuu
方法时,它可用于比较两个Alist
对象。因此,在这里键入“作为边”是没有用的
您可以为Edge
定义richcmp
方法,这意味着可以使用该方法来比较两个Edge
对象。我强烈建议执行所有比较。因为就像现在一样,它可能会表现出一些奇怪的行为,比如
In [1]: e1 = Edge(42)
In [2]: e2 = Edge(42)
In [3]: e3 = Edge(23)
In [4]: e1 < e2
Out[4]: False
# How it is supposed to be
In [5]: e1 == e2
Out[5]: False
# This should be True
In [6]: e1 < e3
Out[6]: True
# How it is supposed to be
In [6]: e1 <= e3
Out[6]: False
# This should be True
[1]中的e1=Edge(42)
[2]中:e2=边(42)
In[3]:e3=边(23)
In[4]:e1Edge
的cdef属性。如果试图从python直接访问cdef类的属性,则会出现错误,除非将只读
(用于读取访问)或公共
(用于读取和写入访问)添加到属性定义中
以此边缘类为例:
cdef class Edge:
cdef readonly int savings # visible from python
cdef int foo # not visible from python
def __init__(self, int s):
self.savings = s
self.foo = 42
def __repr__(self):
"""Friendly representation so we can see how the edges sort."""
return "Edge: {}".format(self.savings)
如果我们编译它(假设它位于文件sortlist.pyx
)并将其导入Python shell中:
In [1]: import sortlist as sl
In [2]: e = sl.Edge(42)
In [3]: e.savings
Out[3]: 23
In [4]: e.foo
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-3ebfe6d526e9> in <module>()
----> 1 e.foo
AttributeError: 'sortlist.Edge' object has no attribute 'foo'
为什么您可能不需要richcmp(现在的位置)
当您在Alist
中定义\uuu\richcmp\uuu
方法时,它可用于比较两个Alist
对象。因此,在这里键入“作为边”是没有用的
您可以为Edge
定义richcmp
方法,这意味着可以使用该方法来比较两个Edge
对象。我强烈建议执行所有比较。因为就像现在一样,它可能会表现出一些奇怪的行为,比如
In [1]: e1 = Edge(42)
In [2]: e2 = Edge(42)
In [3]: e3 = Edge(23)
In [4]: e1 < e2
Out[4]: False
# How it is supposed to be
In [5]: e1 == e2
Out[5]: False
# This should be True
In [6]: e1 < e3
Out[6]: True
# How it is supposed to be
In [6]: e1 <= e3
Out[6]: False
# This should be True
[1]中的e1=Edge(42)
[2]中:e2=边(42)
In[3]:e3=边(23)
In[4]:e1