【发布时间】:2019-01-13 20:37:26
【问题描述】:
我想根据属性的值将类的实例组合在一起。 假设我有以下课程:
class location:
def __init__(self,x_coord,y_coord,text):
self.x_coord=x_coord
self.y_coord=y_coord
self.text=text
def __repr___(self):
return self.text
mylist=[location(1,0,'Date'),location(5,0,'of'),location(8,0,'Entry'), location(28,0,'Date'),location(29,0,'of'),location(30,0,'Birth') ]
如果 x_coord 属性的差异小于 10,我想对我的类列表进行分组,以便
mygroupedlist=[['Date','of','Entry'],['Date','of','Birth']]
谁能给我一个提示?
【问题讨论】:
-
上一个项目少于 10 个,还是所有积累的项目少于 10 个?
[1, 5, 8, 12, 20, 100]将如何分组? -
是的,来自上一项。我们可以假设 mylist 已经按照 x 升序排序了
标签: python python-3.x class oop grouping