list manipulation and recursion

2024/10/6 9:30:50

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 recursionif len(images) == 0: print("finished")return 1idx = 0for 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 removedidx += 1if idx == 5: print("break at idx: ", idx) idx = 0break # This marks the point, grid is filled, proceed with the next pagefillLayout(images)fillLayout(imageSet)

I get the following output:

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

What I want is

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

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

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

https://en.xdnf.cn/q/119997.html

Related Q&A

Getting TypeError: int object is not callable

Getting TypeError: int object is not callable. What am i doing wrong as i just want to add 10 to the z variableprint ("Hello World")x=int(input("Enter X")) y=int(input("Enter Y…

How to create a loop from 1-9 and from a-z?

I currently use:for i in range(1,10):print iWhich prints the digits 1 to 9. But I want to add a-z to the mix. How can I combine them?

Need Python to accept upper and lower case input

I want my Python code to accept both uppercase and lowercase input.Ive tried casefold, but with no luck. Any help?advice ="" while advice !=("Yes"):print("Would you like some…

What is [1] , in sock.getsockname()[1]? [duplicate]

This question already has answers here:How to access List elements(5 answers)Closed 7 years ago.I was going through socket programming in Python and I saw this: sock.getsockname()[1] Can anyone please …

How to use sys.exit() if input is equal to specific number

I am looking to correct this code so that when the user inputs 99999 then the code stops running, im also looking to make it so that if the user input is 999 it sets the total to 0 import sysdef money_…

python about linking sublist with same number together

I need to group sublists with the same elements together For example:list1 =[[1, 0], [2, 1], [30, 32]]would link [1, 0] and [2, 1] together since they both contain 1 and those two would combine into [0…

what to fix to solve the TypeError: an integer is required (got type str) [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 6 years ago.I have a problem with the choice of calculation years. python flux2nc.py ../data/output/fluxes/ …

Multiprocessing in python/beautifulsoup issues

Hi guys im fairly new in python. what im trying to do is to move my old code into multiprocessing however im facing some errors that i hope anyone could help me out. My code is used to check a few thou…

Fastest possible generation of permutation with defined element values in Python

Trying to generate permutations, could be used with generator or produced List of Lists (but maybe I need a lot of memory?) Looked on the Internet and SO, but couldnt find a version where I define the…

Obtain coordinates of a Polygon / Multi-polygon around a point in python [duplicate]

This question already has answers here:Draw a polygon around point in scattermapbox using python(2 answers)Closed 2 years ago.I am using plotlys scattermapbox to draw a polygon around a point object. I…