How to slice in matrixes without using Numpy in Python

I want to slice in the (a) list so that it takes 3 from each list and makes a list of 9, but doesn’t go to the next (a) list until all members of the list have been separated by 9.

import rhinoscriptsyntax as rs

for i in range(2):

a =[[[[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]]]

for t,n in enumerate(a):
    for i,k in enumerate(n):
        for j,w in enumerate(n):
            s = [n[t][i][j] , n[t][i][j+1] , n[t][i][j+2] , n[t][i+1][j] , n[t][i+1][j+1] ,\
            n[t][i+1][j+2] , n[t][i+2][j] , n[t][i+2][j+1] , n[t][i+2][j+2]]

This is the result:

 a =[[[[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]]]

 s = [1, 0, 0, 0, 1, 0, 1, 1, 0]

 a =[[[[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]]]

 s = [1, 0, 0, 0, 1, 0, 1, 1, 0]

But i want this result:

a =[[[[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]]]

 s = [[1, 0, 0, 0, 1, 0, 1, 1, 0],[1, 1, 0, 0, 1, 1, 0, 1, 0]]

 a =[[[[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]]]

 s = [[1, 0, 0, 0, 1, 0, 1, 1, 0],[1, 1, 0, 0, 1, 1, 0, 1, 0]]

more information:

If you simplify your list a then you can do it like this:

a = [[1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 0]]

r = []

for i in range(2):
    l = []
    for x in a:
        step = i*3
        l.extend( x[0+step:3+step])
    r.append(l)
    
print r

There are probably better ways.

Hello,

Modified from on a couple of stackoverflow posts :

def flatten(l):
"""flatten a nested list of lists"""
    for el in l:
        if isinstance(el, list):
            for sub in flatten(el):
                yield sub
        else:
            yield el
            
def chunks(lst, n):
    """list successive n-sized chunks from lst."""
    return [lst[i:i + n] for i in range(0, len(lst), n)]
    
print chunks(list(flatten(a)), 9)

This gives an empty list.

The length of the flattened list is 18, range(18,9) will give [], and you’re missing the bit where you are supposed to take half of each list, concatenate these to get one list, then the other halves of each list, concatenate. Your chunking gives just the first n, then take the next n, but you need to weave them.

yeah it is good way but i want the lists in list (a) to be separated by 3 to 3 first and then your code to run between 3 lists and then the next 3 lists.
and here the problem is …
The number of lists in list (a) isn’t constant and changes with one parameter, and if the number of lists in list (a) is 4, the code must randomly add two more lists with random element in the range of 0 and 1 like (randint(0,1)) to list (a) with the same length of other lists in (a) to be able to 3 to 3 Slide in list (a) and then run your code.

I wrote only a naive version for the input you provided. I leave it as an excercise to you to create a properly parametrized version of this. I only wanted to show how you can use slices to get the parts of the list you need at every stage. It is up to you to determine the correct stepping for the slices based on your inputs.

If you want to simplify the input automatically so you end up with the list as I proposed you could use an adapted version of the flatten function that was proposed by @Dancergraham , with the change that it wouldn’t recurse into a list if the list has no lists as its elements.

2 Likes

Oops I deleted a key bit of the code :D. The logic is much more complicated than I understood but I will fix my version above anyway…

1 Like