ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 29 Sep 2020 06:23:31 +0200Concatenation of listshttps://ask.sagemath.org/question/53643/concatenation-of-lists/Starting from a list of lists and some extra lists,
I want to combine them in various ways.
Suppose I have the following lists:
- a list of lists
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- two extra lists
U = [100, 200, 300]
V = [40, 50, 60]
How to extend `A` using `U` and `V` to obtain
B = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [100, 200, 300]]
C = [[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]
D = [[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300], [40, 50, 60, a]]
for a given `a`.
I have the same question with matrices and vectors.
And is there a mechanism to go back to lists from matrices and vectors?Mon, 28 Sep 2020 18:22:41 +0200https://ask.sagemath.org/question/53643/concatenation-of-lists/Answer by slelievre for <p>Starting from a list of lists and some extra lists,
I want to combine them in various ways.</p>
<p>Suppose I have the following lists:</p>
<ul>
<li><p>a list of lists</p>
<pre><code>A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
</code></pre></li>
<li><p>two extra lists</p>
<pre><code>U = [100, 200, 300]
V = [40, 50, 60]
</code></pre></li>
</ul>
<p>How to extend <code>A</code> using <code>U</code> and <code>V</code> to obtain</p>
<pre><code>B = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [100, 200, 300]]
C = [[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]
D = [[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300], [40, 50, 60, a]]
</code></pre>
<p>for a given <code>a</code>.</p>
<p>I have the same question with matrices and vectors.</p>
<p>And is there a mechanism to go back to lists from matrices and vectors?</p>
https://ask.sagemath.org/question/53643/concatenation-of-lists/?answer=53644#post-id-53644Hoping this answers the question.
Starting point
- a list of lists:
sage: A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- two extra lists
sage: U = [100, 200, 300]
sage: V = [40, 50, 60]
Combined forms
sage: B = A + [U]
sage: B
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [100, 200, 300]]
sage: C = [a + [u] for a, u in zip(A, U)]
sage: C
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]
sage: a = SR.var('a')
sage: D = C + [V + [a]]
sage: D
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300], [40, 50, 60, a]]
Using matrices and vectors:
sage: A = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sage: A
[1 2 3]
[4 5 6]
[7 8 9]
sage: U = vector([100, 200, 300])
sage: U
(100, 200, 300)
sage: B = matrix(A.rows() + [U])
sage: B
[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[100 200 300]
sage: C = A.augment(U)
sage: C
[ 1 2 3 100]
[ 4 5 6 200]
[ 7 8 9 300]
From a matrix to a list of vectors:
sage: C_rows = C.rows()
sage: C_rows
[(1, 2, 3, 100), (4, 5, 6, 200), (7, 8, 9, 300)]
From a matrix to a list of lists:
sage: C_list_of_lists = [list(row) for row in C]
sage: C_list_of_lists
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]Mon, 28 Sep 2020 20:26:24 +0200https://ask.sagemath.org/question/53643/concatenation-of-lists/?answer=53644#post-id-53644Comment by Cyrille for <p>Hoping this answers the question.</p>
<p>Starting point</p>
<ul>
<li><p>a list of lists:</p>
<pre><code>sage: A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
</code></pre></li>
<li><p>two extra lists</p>
<pre><code>sage: U = [100, 200, 300]
sage: V = [40, 50, 60]
</code></pre></li>
</ul>
<p>Combined forms</p>
<pre><code>sage: B = A + [U]
sage: B
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [100, 200, 300]]
sage: C = [a + [u] for a, u in zip(A, U)]
sage: C
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]
sage: a = SR.var('a')
sage: D = C + [V + [a]]
sage: D
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300], [40, 50, 60, a]]
</code></pre>
<p>Using matrices and vectors:</p>
<pre><code>sage: A = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sage: A
[1 2 3]
[4 5 6]
[7 8 9]
sage: U = vector([100, 200, 300])
sage: U
(100, 200, 300)
sage: B = matrix(A.rows() + [U])
sage: B
[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[100 200 300]
sage: C = A.augment(U)
sage: C
[ 1 2 3 100]
[ 4 5 6 200]
[ 7 8 9 300]
</code></pre>
<p>From a matrix to a list of vectors:</p>
<pre><code>sage: C_rows = C.rows()
sage: C_rows
[(1, 2, 3, 100), (4, 5, 6, 200), (7, 8, 9, 300)]
</code></pre>
<p>From a matrix to a list of lists:</p>
<pre><code>sage: C_list_of_lists = [list(row) for row in C]
sage: C_list_of_lists
[[1, 2, 3, 100], [4, 5, 6, 200], [7, 8, 9, 300]]
</code></pre>
https://ask.sagemath.org/question/53643/concatenation-of-lists/?comment=53646#post-id-53646Great answer, very usefull. But obviously impossible to find without your help. Thanks.Tue, 29 Sep 2020 06:23:31 +0200https://ask.sagemath.org/question/53643/concatenation-of-lists/?comment=53646#post-id-53646