用Python建立档案数据索引

2024-06-12 06:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个更为python的方式或更好的做法,以简化过滤数据。基本上,我有类结构化数据,我想选择和分组属性。有没有一个好的方法来做到这一点,而不扁平的表格格式

示例:

class Point:
    def __init__(self, x, y)
        self.x = x
        self.y = y

class Square:
    def __init__(self,origin,length,rotation,color):
        self.origin = origin
        self.length = length
        ...


#populated with all squares
square_list = get_squares()

#create x,y map of all squares
XYindex = defaultdict(list)
for square in square_list:
    XYindex[str(square.origin.x)+','+str(square.origin.y)].append(square)


colorindex = defaultdict(list)
for square in square_list:
    colorindex[str(square.color)].append(square)

#find black squares at 0,0
result = []
for square in colorindex['black']:
    if square in XYindex['0,0']:
        result.append(square)

Tags: 数据inselffordeforiginlengthlist