0

Example: I have a list:

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

And I need to make it look like this but without using numpy.array_split():

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

How can I do it? Not only for this one case, but when I have 4 elements, I want to have 2 and 2, (9 - 3,3,3 and 16 - 4,4,4,4) etc.

  • What do you want to return when the original list has 5 elements? – Mark Lavin Nov 24 '21 at 14:30
  • Maybe this answer is what you need: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – Florin C. Nov 24 '21 at 14:30
  • Do you want to only split lists of length n, where n is a square number? – Haukland Nov 24 '21 at 14:31
  • @qwerty22 SO is not a homework site. Show what you have tried before asking for help in either direction. Votes to Close for lack of effort. End of Review. – ZF007 Nov 24 '21 at 16:34

6 Answers6

2

You can get the square root of the list's length then split it using a list comprehension. This will work for lists with the length of 4, 9, 16, ...:

lst = [8, 3, 4, 1, 5, 9, 6, 7, 2]
lst2 = [8, 3, 4, 1]

def split_equal(lst):
    len_ = len(lst)

    # returns emtpy list, if the list has no item.
    if len_ == 0:
        return []
    
    n = int(len_ ** 0.5)
    return [lst[i:i + n] for i in range(0, len_, n)]

output:

[[8, 3, 4], [1, 5, 9], [6, 7, 2]]
[[8, 3], [4, 1]]
S.B
  • 13,077
  • 10
  • 22
  • 49
1

You can use that:

def splitter(inlist):
    n = len(inlist)
    m = int(n ** 0.5)
    if m*m != n:
        raise Exception("")
    return [[inlist[i+j] for j in range(m)] for i in range(m)]

print(splitter([8, 3, 4, 1]))
print(splitter([8, 3, 4, 1, 5, 9, 6, 7, 2]))
print(splitter([8, 3, 4, 1, 5, 9, 6, 7, 2, 8, 3, 4, 1, 5, 9, 6]))

Result:

[[8, 3], [3, 4]]
[[8, 3, 4], [3, 4, 1], [4, 1, 5]]
[[8, 3, 4, 1], [3, 4, 1, 5], [4, 1, 5, 9], [1, 5, 9, 6]]

Carefull, it will crash if the square of the len of input list is not integer.

Vincent Bénet
  • 1,212
  • 6
  • 20
1
def equal_array_split(arr, split_arr_len):
    array_length = len(arr)
    if array_length % split_arr_len == 0:
        return [arr[i:i+split_arr_len] for i in range(0,array_length,split_arr_len)]
    else:
        return "Invalid split array length!!"


print(equal_array_split([1,2,3,4,5,6,7,8,9],3))
print(equal_array_split([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],4))
print(equal_array_split([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],8))
print(equal_array_split([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],2))

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
[[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]]
Rasin C
  • 49
  • 2
0

You can slice the list with a list comprehension. Assuming the input is a square number length:

import numpy as np

arr = [8, 3, 4, 1, 5, 9, 6, 7, 2]
n = int(np.sqrt(len(arr)))
result = [arr[i*n:(i+1)*n] for i in range(int(n))]
chrispduck
  • 333
  • 1
  • 3
  • 9
0

the split value being a square

list = [8, 3, 4, 1, 5, 9, 6, 7, 2]
result = []
N = split_value #split_value being the value required to split the list
for i in range(0,len(list),N):
    result.append(list[i:i+N])
    
print(result)
AJ31
  • 210
  • 2
  • 13
0

Whitout numpy....

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

splitedSize = 3
a_splited = [a[x:x+splitedSize] for x in range(0, len(a), splitedSize)]

print(a_splited)
BiswajitPaloi
  • 586
  • 4
  • 16