ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 26 Sep 2020 18:28:47 -0500Splitting a list into sub-listhttps://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/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)`.Sat, 26 Sep 2020 03:55:46 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/Answer by slelievre for <p>I have not find the way do do this split <code>A=[1, 2, 3, 4, 6, 8, 2, 7, 9]</code> in such a way to obtain <code>A=[[1, 2, 3], [4, 6, 8], [2, 7, 9]]</code> --- simply for the cazse where <code>len(A)= k len(sublist)</code>.</p>
https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?answer=53606#post-id-53606<!-- lang: python -->
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.Sat, 26 Sep 2020 05:31:04 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?answer=53606#post-id-53606Comment by slelievre for <p>Define a function <code>split_into_k</code> which takes a list <code>l</code>
of length <code>n</code> and an integer <code>k</code> and splits the list <code>l</code>
into a list of <code>k</code> sublists of length <code>n // k</code>.</p>
<pre><code>def split_into_k(l, k):
n = len(l)
m = n // k
return [l[i*m:(i+1)*m] for i in range(k)]
</code></pre>
<p>Examples with a list of length 12:</p>
<pre><code>sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]
</code></pre>
<p>Split it into 3 or 4:</p>
<pre><code>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]]
</code></pre>
<p>If <code>k</code> does not divide <code>len(l)</code>, the trailing part
is removed.</p>
<pre><code>sage: split_into_k(A, 5)
[[1, 2], [3, 4], [6, 8], [2, 7], [9, 0]]
</code></pre>
<p>Other options would be to raise an exception
or to have a shorter last part.</p>
<p>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.</p>
<p>Examples using the list <code>A</code> above:</p>
<pre><code>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]
</code></pre>
<p>This opens up other ways to write the function <code>split_into_k</code>.</p>
<p>For instance:</p>
<pre><code>def split_into_k(A, k):
return [list(row) for row in matrix(k, len(A) // k, A)]
</code></pre>
<p>Example:</p>
<pre><code>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]]
</code></pre>
<p>Note that the resulting lists might have slight differences,
as all elements will now be part of a same ring.</p>
<p>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 <code>split_into_k</code> will now be rationals.</p>
https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53610#post-id-53610@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.Sat, 26 Sep 2020 10:07:44 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53610#post-id-53610Comment by slelievre for <p>Define a function <code>split_into_k</code> which takes a list <code>l</code>
of length <code>n</code> and an integer <code>k</code> and splits the list <code>l</code>
into a list of <code>k</code> sublists of length <code>n // k</code>.</p>
<pre><code>def split_into_k(l, k):
n = len(l)
m = n // k
return [l[i*m:(i+1)*m] for i in range(k)]
</code></pre>
<p>Examples with a list of length 12:</p>
<pre><code>sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]
</code></pre>
<p>Split it into 3 or 4:</p>
<pre><code>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]]
</code></pre>
<p>If <code>k</code> does not divide <code>len(l)</code>, the trailing part
is removed.</p>
<pre><code>sage: split_into_k(A, 5)
[[1, 2], [3, 4], [6, 8], [2, 7], [9, 0]]
</code></pre>
<p>Other options would be to raise an exception
or to have a shorter last part.</p>
<p>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.</p>
<p>Examples using the list <code>A</code> above:</p>
<pre><code>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]
</code></pre>
<p>This opens up other ways to write the function <code>split_into_k</code>.</p>
<p>For instance:</p>
<pre><code>def split_into_k(A, k):
return [list(row) for row in matrix(k, len(A) // k, A)]
</code></pre>
<p>Example:</p>
<pre><code>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]]
</code></pre>
<p>Note that the resulting lists might have slight differences,
as all elements will now be part of a same ring.</p>
<p>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 <code>split_into_k</code> will now be rationals.</p>
https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53616#post-id-53616@Cyrille, thanks for the pointer to the execandshare dot fun website. I'll explore.Sat, 26 Sep 2020 18:28:47 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53616#post-id-53616Comment by slelievre for <p>Define a function <code>split_into_k</code> which takes a list <code>l</code>
of length <code>n</code> and an integer <code>k</code> and splits the list <code>l</code>
into a list of <code>k</code> sublists of length <code>n // k</code>.</p>
<pre><code>def split_into_k(l, k):
n = len(l)
m = n // k
return [l[i*m:(i+1)*m] for i in range(k)]
</code></pre>
<p>Examples with a list of length 12:</p>
<pre><code>sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]
</code></pre>
<p>Split it into 3 or 4:</p>
<pre><code>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]]
</code></pre>
<p>If <code>k</code> does not divide <code>len(l)</code>, the trailing part
is removed.</p>
<pre><code>sage: split_into_k(A, 5)
[[1, 2], [3, 4], [6, 8], [2, 7], [9, 0]]
</code></pre>
<p>Other options would be to raise an exception
or to have a shorter last part.</p>
<p>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.</p>
<p>Examples using the list <code>A</code> above:</p>
<pre><code>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]
</code></pre>
<p>This opens up other ways to write the function <code>split_into_k</code>.</p>
<p>For instance:</p>
<pre><code>def split_into_k(A, k):
return [list(row) for row in matrix(k, len(A) // k, A)]
</code></pre>
<p>Example:</p>
<pre><code>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]]
</code></pre>
<p>Note that the resulting lists might have slight differences,
as all elements will now be part of a same ring.</p>
<p>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 <code>split_into_k</code> will now be rationals.</p>
https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53615#post-id-53615@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.Sat, 26 Sep 2020 18:14:31 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53615#post-id-53615Comment by Cyrille for <p>Define a function <code>split_into_k</code> which takes a list <code>l</code>
of length <code>n</code> and an integer <code>k</code> and splits the list <code>l</code>
into a list of <code>k</code> sublists of length <code>n // k</code>.</p>
<pre><code>def split_into_k(l, k):
n = len(l)
m = n // k
return [l[i*m:(i+1)*m] for i in range(k)]
</code></pre>
<p>Examples with a list of length 12:</p>
<pre><code>sage: A = [1, 2, 3, 4, 6, 8, 2, 7, 9, 0, -1, -2]
</code></pre>
<p>Split it into 3 or 4:</p>
<pre><code>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]]
</code></pre>
<p>If <code>k</code> does not divide <code>len(l)</code>, the trailing part
is removed.</p>
<pre><code>sage: split_into_k(A, 5)
[[1, 2], [3, 4], [6, 8], [2, 7], [9, 0]]
</code></pre>
<p>Other options would be to raise an exception
or to have a shorter last part.</p>
<p>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.</p>
<p>Examples using the list <code>A</code> above:</p>
<pre><code>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]
</code></pre>
<p>This opens up other ways to write the function <code>split_into_k</code>.</p>
<p>For instance:</p>
<pre><code>def split_into_k(A, k):
return [list(row) for row in matrix(k, len(A) // k, A)]
</code></pre>
<p>Example:</p>
<pre><code>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]]
</code></pre>
<p>Note that the resulting lists might have slight differences,
as all elements will now be part of a same ring.</p>
<p>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 <code>split_into_k</code> will now be rationals.</p>
https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53608#post-id-53608Slelievre, 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.Sat, 26 Sep 2020 07:56:34 -0500https://ask.sagemath.org/question/53605/splitting-a-list-into-sub-list/?comment=53608#post-id-53608