Python: Sort a list into a grid

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python: Sort a list into a grid



I'm building a GUI in Python and Tkinter and I need to generate a graphic for every item on a list. My list is going to vary in length from say 2 to 20. How can I sort my list into the most efficient grid?



for example:


myList = [0,1,2,3,4,5,6]



len(myList) will return 7, so the best grid is probably 3x3 with one graphic on the last row.



Like so: Best Grid



EDIT: Thank you all for your help. This GUI is going to be full screen on a 800x480 RaspberryPi touchscreen, so I want to make the most efficient use of space in Landscape mode as I can.



This is what I've tried so far:


import math

myList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]#,16,17,18,19,20,21,23,24,25]

def integer_sqrt(n):
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x

a = integer_sqrt(len(myList))
c = len(myList)

print("The length of myList is: ".format(len(myList)))
print("The integer-only root of the length of myList is: ".format(a))
print("The remainder of the calculation is: ".format(c % a))



The output for a short list seems to work:


>>> The length of myList is: 5
>>> The integer-only root of the length of myList is: 2
>>> The remainder of the calculation is: 1



This works too:


myList = [1,2,3,4,5,6,7,8,9,10]

>>> The length of myList is: 10
>>> The integer-only root of the length of myList is: 3
>>> The remainder of the calculation is: 1



But when I change the list length to a higher modulo, it starts to fail:


myList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

>>> The length of myList is: 15
>>> The integer-only root of the length of myList is: 3
>>> The remainder of the calculation is: 0



I'm not sure why this doesn't work, and I'm not married to this method of sorting. Is there a better way?





What does "sort into a grid" mean? If your list had 12 items, should it be 3x4 or 4x3?
– roganjosh
Aug 11 at 21:47






Isn't the best grid for 7 items a 2x4 which makes 8 positions, leaving only 1 empty rather than 2 in a 3x3?
– N Chauhan
Aug 11 at 21:51


2x4


3x3





Or technically 1x7 but I don't think that's what they want.
– The Tesseract's Shadow
Aug 11 at 21:52


1x7





You've said what you are doing but you haven't asked a question. What part of the problem do you need help with? Have you worked through a tkinter tutorial and searched this site for other questions related to displaying data in a grid?
– Bryan Oakley
Aug 11 at 21:53





What if you did min(i for i in range(len(mylist)) if i**2 > len(mylist)) which would give the smallest square number larger than the number of items... then work your way from there? changing values in a systematic way so that the grid is as close to a square as possible, yet wastes the least space.
– N Chauhan
Aug 11 at 21:55



min(i for i in range(len(mylist)) if i**2 > len(mylist))




1 Answer
1



I figured out why it isn't working. I added the following lines:


length = len(myList)
rows = a
columns = c//a
remainder = c % a

print("The integer of that modulo calc is: ".format(c//a))
print("So rows by columns can fit a list of with remaining".format(rows,columns,length,remainder))



and got this:


myList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

>>> The length of myList is: 24
>>> The integer only root of the length of myList is: 4
>>> The remainder of the calculation is: 4
>>> The integer of that modulo calc is: 6
>>> So 4 rows by 6 columns can fit a list of 24 with 0 remaining



This gives me all of the values that I need to map my grid. I'm still open to better ways if anyone knows one.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered