如何基于Python中的另一个集合对集合进行排序?

车道

我是Python的新手,我需要以下问题的帮助。我在下面发布的此定义将采用表示矩形Ex的元组的集合。(宽度,高度)。

在该方法中,我首先对矩形集合进行排序,以使宽度最大的元素位于列表的开头,然后再使用此集合为每个矩形分配UPPER_LEFT_X和UPPER_LEFT_Y坐标。

我的问题是,如何获取此坐标集合并以与original_rectangle_collection完全相同的顺序对其进行排序?驱动程序的工作方式是,必须以与原始矩形集合相同的顺序给出确定的坐标。谢谢!让我知道是否需要更多说明。

def find_best_coordinates(rectangles):
    placement = []
    upper_left_x = 0
    upper_left_y = 0
    loop_count = 0

    original_rectangle_collection = rectangles
    #sort the rectangles according to width.
    rectangles.sort(key=operator.itemgetter(0))
    #reverse the list so the tallest rectanlgle is placed first
    rectangles.reverse()


    #set the max width to the tallest rectangle's width
    max_width = rectangles[0][0]
    #print("Max width",max_width)

    #loop through and place the rectangles
    for rectangle in rectangles:
        if loop_count == 0:
            max_width = rectangle[0]
        height = rectangle[1]
        coordinate = (upper_left_x, upper_left_y)  
        placement.insert(0, coordinate)             
        upper_left_y = upper_left_y - height - 990
        loop_count = loop_count + 1
        if loop_count == 50:
            upper_left_x = upper_left_x + max_width + 990
           # print("x = ", upper_left_x)
            loop_count = 0
            #print("y = ", upper_left_y)
            upper_left_y = 0

    #reverse the list before it gets returned
    placement.reverse()                            
    return placement
布伦丹·亚伯(Brendan Abel)

您可以通过始终将原始索引与矩形一起传递,并使其与生成的坐标保持一致的方式来执行此操作。

# Store indexes
rdicts = [{'index': i, 'rectangle': r} for i, r in enumerate(rectangles)]
# Sort by width
rdicts.sort(key=lambda d: d['rectangle'][0], reverse=True)

placement = []
for rdict in rdicts:
    rectangle = rdict['rectangle']
    ... # do the rest of your algorithm
    # Add the calculated coordinate, along with the original index
    placement.append({'index': rdict['index'], 'coordinate': coordinate})

# Sort by the original index
placement.sort(key=lambda p: p['index'])
# Return the coordinates without the indexes.
ordered_placement = [p['coordinate'] for p in placement]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据另一个集合的元素对集合进行特定排序?

如何从另一个集合中获取基于列的集合?

如何确定一个集合是否包含Python中的另一个集合

如何根据另一个集合和其他条件对一个集合进行排序

基于另一个集合中的条件聚合

根据项目在另一个集合中引用的次数对项目进行排序

以“Scala方式”根据另一个集合对值集合进行排序

如何在 Laravel 中比较和更新基于另一个集合的 Eloquent 集合

MongoDB:基于另一个集合从一个集合中进行条件选择

如何在另一个集合中拥有一个集合?

如何在firebase中从一个集合访问另一个集合

从集合中获取另一个元素(Python)

根据另一个集合中的ID对mongodb集合项进行排名

为什么EF无法按另一个集合的顺序对项目进行排序以及如何解决?

如何获取Firebase中另一个集合的文档中的集合

如何使用mongodb中主集合的objectID在另一个集合中查找

按另一个数组对集合进行排序

按另一个数组对 Laravel 集合进行排序

通过另一个集合排序

如何使用另一个集合中的信息过滤MongoDB集合?

如何将集合添加到Cloud Firestore中的另一个集合?

如何将集合复制到 mongoDB 中的另一个新集合

将MongoDB(3.0)集合的子集保存到Python中的另一个集合

如何使用集合理解在集合中查找不在另一个集合中的单个元素

如何从一个集合中获取数据并插入到 Nodejs 中的另一个集合中?

Python Regex [Forking]-基于术语捕获组,但是如果遇到集合中的另一个术语则跳过

如何在一个集合中查找未被另一个集合中的文档引用的MongoDB文档?

如何删除一个集合中的引用文档及其从另一个引用集合中的记录

如何查找一个dstore集合中而不是另一个dstore集合中的项目?