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.Thu, 02 Jul 2020 04:31:53 -0500How to organize a list according to an other listhttps://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/Here is a problem that resists all my tentatives. Say I have 2 lists of same length
X=[1.1, 2.5, 5.0, 6.01, 12.5, 18.4, 7.8, 14.9, 20, 13.6]
Y=[13.1, 21.5, 53.0, 16.21, 2.45, 8.94, 4.18, 12.8, 17.7, 13.6]
The first one has already been organized in sublists
Xc=[[12.5, 5.0, 20], [7.8, 1.1, 18.4], [6.01, 14.9, 2.5, 13.6]]
What I want to achieve is to construct a list `Yc` composed of sublists such that if `X [i]` belongs to `Xc[j]`, `Y[i]` belongs to `Yc[j]`.
**Ps** I know that the title is a bad description of what I want to achieve.Thu, 02 Jul 2020 02:02:26 -0500https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/Answer by tmonteil for <p>Here is a problem that resists all my tentatives. Say I have 2 lists of same length</p>
<pre><code>X=[1.1, 2.5, 5.0, 6.01, 12.5, 18.4, 7.8, 14.9, 20, 13.6]
Y=[13.1, 21.5, 53.0, 16.21, 2.45, 8.94, 4.18, 12.8, 17.7, 13.6]
</code></pre>
<p>The first one has already been organized in sublists</p>
<pre><code>Xc=[[12.5, 5.0, 20], [7.8, 1.1, 18.4], [6.01, 14.9, 2.5, 13.6]]
</code></pre>
<p>What I want to achieve is to construct a list <code>Yc</code> composed of sublists such that if <code>X [i]</code> belongs to <code>Xc[j]</code>, <code>Y[i]</code> belongs to <code>Yc[j]</code>.</p>
<p><strong>Ps</strong> I know that the title is a bad description of what I want to achieve.</p>
https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?answer=52299#post-id-52299For every sublist of `Yc` and every element `a` in `X`, search `a` in the sublist and append the corresponding element `b` in `Y` (`X` and `Y` are aligned with `zip`):
Yc = []
for j in Xc:
l = []
for (a,b) in zip(X,Y):
if a in j:
l.append(b)
Yc.append(l)
Note that this way, the elements in the sublists are not sorted in the same order (but i guess you do not mind given the description).
Thu, 02 Jul 2020 02:45:48 -0500https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?answer=52299#post-id-52299Comment by tmonteil for <p>For every sublist of <code>Yc</code> and every element <code>a</code> in <code>X</code>, search <code>a</code> in the sublist and append the corresponding element <code>b</code> in <code>Y</code> (<code>X</code> and <code>Y</code> are aligned with <code>zip</code>):</p>
<pre><code>Yc = []
for j in Xc:
l = []
for (a,b) in zip(X,Y):
if a in j:
l.append(b)
Yc.append(l)
</code></pre>
<p>Note that this way, the elements in the sublists are not sorted in the same order (but i guess you do not mind given the description).</p>
https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?comment=52302#post-id-52302you can always hide loops, for example by replacing them with recursive calls of functions.Thu, 02 Jul 2020 04:31:53 -0500https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?comment=52302#post-id-52302Comment by Cyrille for <p>For every sublist of <code>Yc</code> and every element <code>a</code> in <code>X</code>, search <code>a</code> in the sublist and append the corresponding element <code>b</code> in <code>Y</code> (<code>X</code> and <code>Y</code> are aligned with <code>zip</code>):</p>
<pre><code>Yc = []
for j in Xc:
l = []
for (a,b) in zip(X,Y):
if a in j:
l.append(b)
Yc.append(l)
</code></pre>
<p>Note that this way, the elements in the sublists are not sorted in the same order (but i guess you do not mind given the description).</p>
https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?comment=52301#post-id-52301Thanks a lot tmonteil. It works as expected. Simply for my understanding of programming. Was it possible to do the same thing withou loops ?Thu, 02 Jul 2020 04:17:05 -0500https://ask.sagemath.org/question/52297/how-to-organize-a-list-according-to-an-other-list/?comment=52301#post-id-52301