列表操作和递归 [英] list manipulation and recursion

查看:64
本文介绍了列表操作和递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pdf页面中有一个mansory-grid.网格是随机选择的,所以我不知道我必须填充多少个垂直单元格或交叉单元格.在我的列表中,我具有要继续处理的所有图像,每个图像都是直立或交叉的.我现在的方法是:

I have a mansory-grid in a pdf-page. The grid is choosen randomly, so i do not know how much upright cells or cross cells I have to fill. In my list I have all images that I want to proceed, each marked if it is upright or cross. My approach is now:

  1. 获取页面的网格
  2. 遍历列表并使用适合下一个网格单元的图像.
  3. 从列表中删除该图像
  4. 继续下一个单元格.
  5. 如果页面上的网格已填充,请继续下一页(步骤1)

为了测试我的方法,我使用了以下脚本:

To test my approach, I used the following script:

imageSet = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
def fillLayout(images):
    print("Images in Stack", len(images))
    # Base condition to leave recursion
    if len(images) == 0: 
        print("finished")
        return 1

    idx = 0
    for image in images: 
        print(" index: ", idx, "item: ", image)
        del(images[idx]) # This marks the point, image is used on the cell layout and can be removed
        idx += 1
        if idx == 5: 
            print("break at idx: ", idx) 
            idx = 0
            break # This marks the point, grid is filled, proceed with the next page

    fillLayout(images)

fillLayout(imageSet)

我得到以下输出:

Images in Stack 16
 index:  0 item:  1
 index:  1 item:  3
 index:  2 item:  5
 index:  3 item:  7
 index:  4 item:  9
break at idx:  5
Images in Stack 11
 index:  0 item:  2
 index:  1 item:  6
 index:  2 item:  10
 index:  3 item:  12
 index:  4 item:  14
break at idx:  5
Images in Stack 6
 index:  0 item:  4
 index:  1 item:  11
 index:  2 item:  15
Images in Stack 3    <-- from now it does not proceed as expected
 index:  0 item:  8
 index:  1 item:  16
Images in Stack 1
 index:  0 item:  13
Images in Stack 0
finished

我想要的是

...
Images in Stack 6
 index:  0 item:  4
 index:  1 item:  11
 index:  2 item:  15
 index:  0 item:  8
 index:  1 item:  16
break at idx:  5
Images in Stack 1
 index:  0 item:  13
finished

任何想法,我缺少什么,或如何解决我的问题.

Any ideas what I am missing, or how to solve my problem.

推荐答案

对于我来说,目前尚不清楚,是否有意在每次通过时跳过项目,或者这是代码的副作用,是否是偶然的.也就是说,对我来说,这似乎是一个错误,但也许它是功能而不是错误.

It's somewhat unclear to me if you are intentionally trying to skip items on each pass or if that's accidental on your part as a side effect of your code. That is, it looks like a bug to me, but maybe it's a feature and not a bug.

如果我想从列表中作为一个组删除5个项目并对其进行处理,然后递归该函数,则可以这样做:

If I wanted to remove 5 items from a list as a group and do something with them then recurse the function, I'd do this:

def fill_layout(images):
    out_items = []
    while len(out_items) <5 and images:
        out_items.append(images.pop(0))
    # do something to your 5 or less items in out_items
    if images:
        fill_layout(images)

请注意,您无需递归,只需处理函数中的所有内容即可.此外,您可以将列表切成5个长度,然后随手处理每个长度.您的方法有很多人为的复杂性,我不知道您的示例实际上需要多少—因此,我这样做是为了保留5个一组,从列表中删除并递归.可能有更简单的方法来做您想要的事情.

Note, you don't need to recurse, you could just handle everything in the function. Further, you could just slice the list into 5 lengths and handle each one as you go. There is a lot of artificial complexity in your method and I don't know how much is actually needed from your example -- so I did this to keep the groups of 5, remove from list and recurse. There are probably simpler ways to do what you want.

这篇关于列表操作和递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆