Ask Your Question
0

Splitting a list into sub-list

asked 2020-09-26 10:55:46 +0100

Cyrille gravatar image

updated 2020-09-26 14:24:41 +0100

I have not find the way do do this split A=[1, 2, 3, 4, 6, 8, 2, 7, 9] in such a way to obtain A=[[1, 2, 3], [4, 6, 8], [2, 7, 9]] --- simply for the cazse where len(A)= k len(sublist).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-09-26 12:31:04 +0100

slelievre gravatar image

updated 2020-09-30 14:22:16 +0100

Define a function split_into_k which takes a list l of length n and an integer k and splits the list l into a list of k sublists of length n // k.

def split_into_k(l, k):
    n = len(l)
    m = n // k
    return [l[i*m:(i+1)*m] for i in range(k)]

Examples with a list of length 12:

sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]

Split it into 3 or 4:

sage: split_into_k(A, 3)
[[1, 2, 3, 4], [6, 8, 2, 7], [9, 0, -1, -2]]
sage: split_into_k(A, 4)
[[1, 2, 3], [4, 6, 8], [2, 7, 9], [0, -1, -2]]

If k does not divide len(l), the trailing part is removed.

sage: split_into_k(A, 5)
[[1, 2], [3, 4], [6, 8], [2, 7], [9, 0]]

Other options would be to raise an exception or to have a shorter last part.

In case the goal is to construct a matrix: if provided the desired matrix size, the matrix constructor can eat a non-nested list of values.

Examples using the list A above:

sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]
sage: a = matrix(ZZ, 3, 4, A)
sage: a
[ 1  2  3  4]
[ 6  8  2  7]
[ 9  0 -1 -2]

sage: b = matrix(ZZ, 4, 3, A)
sage: b
[ 1  2  3]
[ 4  6  8]
[ 2  7  9]
[ 0 -1 -2]

This opens up other ways to write the function split_into_k.

For instance:

def split_into_k(A, k):
    return [list(row) for row in matrix(k, len(A) // k, A)]

Example:

sage: split_into_k(A, 3)
[[1, 2, 3, 4], [6, 8, 2, 7], [9, 0, -1, -2]]
sage: split_into_k(A, 4)
[[1, 2, 3], [4, 6, 8], [2, 7, 9], [0, -1, -2]]

Note that the resulting lists might have slight differences, as all elements will now be part of a same ring.

For instance if there was one rational but non-integer entry in the initial list, all elements of the nested lists returned by this version of split_into_k will now be rationals.

edit flag offensive delete link more

Comments

Slelievre, 3 points : first I think this is a very usefull function. It should be part of Sagemath algebra (Its already done for a long time in Mathematica. Second you help me very often when I do not ask stupid questions. I would like to thank you in my work (up now mainly teaching because students need more help than in the past). I need your first name and if you want to know what I am doing, here is a page where you will be able to find my work (every comments is welcome).

https://execandshare.fun

mot de passe : Cons#2020-YS

my main contribution is about lp. But I have contributed also in the analysis of some games.

Cyrille gravatar imageCyrille ( 2020-09-26 14:56:34 +0100 )edit

@Cyrille, regarding the first point: what is the corresponding function in Mathematica?

It is possible that NumPy has a way of doing that.

What is the intended use case?

I edited my answer to show how the matrix constructor can use a flat list.

slelievre gravatar imageslelievre ( 2020-09-26 17:07:44 +0100 )edit

@Cyrille, regarding the other point: click my username in any of my answers or comments.

My Ask Sage profile links to various pages including my professional webpage.

That should have all the information needed to refer to me or contact me.

slelievre gravatar imageslelievre ( 2020-09-27 01:14:31 +0100 )edit

@Cyrille, thanks for the pointer to the execandshare dot fun website. I'll explore.

slelievre gravatar imageslelievre ( 2020-09-27 01:28:47 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-09-26 10:55:46 +0100

Seen: 762 times

Last updated: Sep 30 '20