Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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. get the grid for the page
  2. iterate through the list and use the images which fit to the next grid-cell.
  3. remove this image from the list
  4. Proceed with the next cell.
  5. if the grid on the page is filled proceed with the next page (Step 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)

I get the following output:

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

What I want is

...
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
280 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...