0

lets say i have this list:

all list is:  [512, 512, 2048, 512, 2048, 2048, 2048, 512, 256]

and i have number k who represent the length of the list. what i try to get is to split the list, where the length of the list of lists is the squre of number k, so this is what i need to get:

[[512, 512, 2048], [512, 2048, 2048],[2048, 512, 256]]

after that i want to sort al sub list

for i in range(math.ceil(math.sqrt(k)):
sort(list[i]

what is the best way to do it?

jacob
  • 29
  • 1
  • 6
  • Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) And just use `math.sqrt` to get the square root. – wjandrea Jun 25 '20 at 01:48

1 Answers1

0

I want to mention first off that you are asking for a 2-D array whose length is equivalent to k*k, but in your example your 2-D array has a length of 3, which has not clean square root. That being said, to achieve the above you can do something like the following:

def chunk(iterable, k):
    n = k**2
    for i in range(0, len(iterable), n):
        yield iterable[i:i + n]


values = [512, 512, 2048, 512, 2048, 2048, 2048, 512]
k = 2

chunked = []
for piece in chunk(values, k):
    chunked.append(piece)

print(chunked)
>>> [[512, 512, 2048, 512], [2048, 2048, 2048, 512]]

I can't claim this is "the best way", but it is a way of achieving your goal.

EDIT From Comments by OP

The goal was to make the smaller lists equivalent to the length of square root of k, instead of the sqaure.

import math

def chunk(iterable, k):  
    for i in range(0, len(iterable), math.ceil(math.sqrt(k))): 
        yield sorted(iterable[i:i + math.ceil(math.sqrt(k))])
gallen
  • 1,252
  • 1
  • 18
  • 25
  • EDIT: This was in response to a now deleted comment. The error means somewhere you have something like `zeroListMaxTail(...)`, which is a call but the thing you are attempting to call is a list. I would examine your code, because that's just a syntax issue. To map your names to my code, `splitZeroListOfMaxTail` would replace the `chunk` method, and the rest you seem to have aligned. – gallen Jun 25 '20 at 01:15
  • first thanks for your help. i got this: [[512, 512, 512, 512, 512, 512, 512, 512, 512]] you know whay? – jacob Jun 25 '20 at 01:19
  • If you sorted first, and didn't iterate over the call to `chunk()`, that could result. That function is a generator, and requires you to iterate over it's return value to get all of the smaller lists. Otherwise, I would need to see more of your implementation to give a clear answer, as I'm working on assumption from a sample output. – gallen Jun 25 '20 at 01:21
  • 1
    now its work. i change your method to: def ListOfListBySizeOfQuality(iterable, k): import math for i in range(0, len(iterable), math.ceil(math.sqrt(k))): yield sorted(iterable[i:i + math.ceil(math.sqrt(k))]) – jacob Jun 25 '20 at 01:29
  • Ah, you needed the square root of k, not the square. Your question is asking for the square of k. I'd clarify that in your question and I'll update that in my answer to reflect your change. – gallen Jun 25 '20 at 01:32