1

I have a list which is [1 x 29,584] in size, made up of the product of 172 input groups to 172 output groups. I need to split this list into 172 smaller lists of size [1 x 172]. The ordering is such that for the first smaller list, I need the 1st value, then the 173rd value, then the 345th value etc.

So far I have a preliminary function which does give me the desired list for the first set of values. However, it doesn't work for any subsequent values. I am also trying to keep all my functions in a separate file from the main code, and I am not sure how to call this function to ensure all the different groups get indexed correctly?

large_array_size = 172*172
small_array_size = 172

for i in [0,small_array_size]:
    group_i = functions.collapse_list(results,large_array_size)

In functions file:

def collapse_list(results,large_array_size):
    for i in [0,large_array_size]:
        new_list_i = []
        new_list_i = results[::(172+i)]
    return(new_list_i)

I need to end up with lists group_1, group_2,..., group_172, each of which is made up of 172 values. Any help would be much appreciated.

MUD
  • 121
  • 1
  • 13
  • your `new_list_i = []` is inside loop, keep it out of the loop. It is getting reinitialized every time. Also the implementation seems not correct. – Epsi95 Feb 04 '21 at 14:54
  • Do you expect `new_list_i` to assign different variables like `new_list_0`, `new_list_1` etc.? It doesn't work that way – Tomerikoo Feb 04 '21 at 15:20
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Tomerikoo Feb 04 '21 at 15:20

4 Answers4

1

You would better use a dictionary for this, otherwise you will have problem naming all these lists. Here is a one line solution ('mylist' is your original list):

res={'group_'+str(k+1): mylist[k::172] for k in range(172)}
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
1

If you want to cut up the large list and store a list of smaller lists, the easiest way is with a comprehension.Here's an example with smaller numbers to test the output

small_array_size = 5
large_array_size = small_array_size**2

large_list = list(range(large_array_size))
small_lists = [[large_list[j*small_array_size+i] for j in range(small_array_size)]
               for i in range(small_array_size)]

print(large_list)
print(small_lists)

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

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

ScienceSnake
  • 608
  • 4
  • 15
  • Hi, thanks for your response. This is sort of what I need to do but I need to cut up the larger array differently - in this example I would need every fifth value. So the first smaller array would be [0,5,10,15,20] – MUD Feb 04 '21 at 15:17
  • 1
    Sorry, I misunderstood. In that case, just swap the i and j around inside the nested comprehension. I've modified the answer to reflect that. – ScienceSnake Feb 04 '21 at 15:20
1

Here is an example of a using smaller size. Say we have 100 numbers in the large array and want to split into small groups of 10 using the same algo described in the question.

large_array_size = 10*10
small_array_size = 10

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

groups = []


for i in range(small_array_size):
    group = []
    for j in range(small_array_size):
        group.append(arr[small_array_size*j + i])
    groups.append(group)

print(group)
        

The output of the above code is:

[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
[2, 12, 22, 32, 42, 52, 62, 72, 82, 92]
[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]
[5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
[6, 16, 26, 36, 46, 56, 66, 76, 86, 96]
[7, 17, 27, 37, 47, 57, 67, 77, 87, 97]
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
[9, 19, 29, 39, 49, 59, 69, 79, 89, 99]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

For your case you just have to replace 10 by 172 and then you can reuse this.

Kashish Khullar
  • 394
  • 1
  • 3
  • 14
0

If I understood you correctly you need to pass the index from first loop

large_array_size = 172*172
small_array_size = 172

groups = []
for i in [0,small_array_size]:
    groups.append(functions.collapse_list(results,large_array_size, i))

Then in the function, get the appropriate element

def collapse_list(results,large_array_size, index):
    #for i in [0,large_array_size]:
        # new_list_i = []
    new_list_i = results[index::172]
    return(new_list_i)
Epsi95
  • 8,832
  • 1
  • 16
  • 34
  • 2
    group_i =... will not have the output you desire. It will not create 172 different lists, but only one (named 'group_i') – IoaTzimas Feb 04 '21 at 15:18
  • 1
    @IoaTzimas thank you for identifying that, I actually changed only the needed portion and rest copy pasted from OPs code. My bad. – Epsi95 Feb 04 '21 at 15:22