-1

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?

Virgilio
  • 41
  • 1
  • 7
  • 1
    What does "sort into a grid" mean? If your list had 12 items, should it be 3x4 or 4x3? – roganjosh Aug 11 '18 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 '18 at 21:51
  • 2
    Or technically `1x7` but I don't think that's what they want. – Aeolus Aug 11 '18 at 21:52
  • 1
    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 '18 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 '18 at 21:55
  • I picked up that integer-only root formula from here -[Link](https://stackoverflow.com/questions/15390807/integer-square-root-in-python) – Virgilio Aug 12 '18 at 00:33

1 Answers1

0

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.

Virgilio
  • 41
  • 1
  • 7