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.Thu, 03 Mar 2022 18:44:11 +0100How to select a serie of sublists according to an other listhttps://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/ One more time this is certainly the result of my lack of expertise in programming but I encounter the following problem.
I have a list of lists
A=[[3, 2, 1, 1, 0, 0, 0, 5], [4, 3, -1, 0, -1, 1, 0, 4], [1, 2, 2, 0, 0, 0, 1, 2]]
and an other list
B=[3,5]
What I want is to select a sublist of the list A according to the following criterion : keep the sublist `A[i]` if `A[i][j]==1 for j in B` Wed, 02 Mar 2022 21:55:00 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/Comment by John Palmieri for <p>One more time this is certainly the result of my lack of expertise in programming but I encounter the following problem.</p>
<p>I have a list of lists</p>
<pre><code>A=[[3, 2, 1, 1, 0, 0, 0, 5], [4, 3, -1, 0, -1, 1, 0, 4], [1, 2, 2, 0, 0, 0, 1, 2]]
</code></pre>
<p>and an other list </p>
<pre><code>B=[3,5]
</code></pre>
<p>What I want is to select a sublist of the list A according to the following criterion : keep the sublist <code>A[i]</code> if <code>A[i][j]==1 for j in B</code> </p>
https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61380#post-id-61380This is a bit ambiguous, as revealed by the proposed answer: do you mean "for all j in B" or "for some j in B"?Thu, 03 Mar 2022 18:44:11 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61380#post-id-61380Answer by Max Alekseyev for <p>One more time this is certainly the result of my lack of expertise in programming but I encounter the following problem.</p>
<p>I have a list of lists</p>
<pre><code>A=[[3, 2, 1, 1, 0, 0, 0, 5], [4, 3, -1, 0, -1, 1, 0, 4], [1, 2, 2, 0, 0, 0, 1, 2]]
</code></pre>
<p>and an other list </p>
<pre><code>B=[3,5]
</code></pre>
<p>What I want is to select a sublist of the list A according to the following criterion : keep the sublist <code>A[i]</code> if <code>A[i][j]==1 for j in B</code> </p>
https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?answer=61365#post-id-61365It's quite straightforward:
[a for a in A if all(a[j]==1 for j in B)]Thu, 03 Mar 2022 02:47:49 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?answer=61365#post-id-61365Comment by Max Alekseyev for <p>It's quite straightforward:</p>
<pre><code>[a for a in A if all(a[j]==1 for j in B)]
</code></pre>
https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61379#post-id-61379My answer solves the problem you posed. If the answer is different from expected, then you posed the problem incorrectly.Thu, 03 Mar 2022 18:05:35 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61379#post-id-61379Comment by David Coudert for <p>It's quite straightforward:</p>
<pre><code>[a for a in A if all(a[j]==1 for j in B)]
</code></pre>
https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61368#post-id-61368It seems that you search for the sublists of A containing an element of B. If it is effectively the case, then try
sage: [a for a in A if any(aj in B for aj in a)]
[[3, 2, 1, 1, 0, 0, 0, 5], [4, 3, -1, 0, -1, 1, 0, 4]]Thu, 03 Mar 2022 09:04:29 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61368#post-id-61368Comment by Cyrille for <p>It's quite straightforward:</p>
<pre><code>[a for a in A if all(a[j]==1 for j in B)]
</code></pre>
https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61367#post-id-61367On my computer the effect of your proposal is an empty list when it should be
`[[3, 2, 1, 1, 0, 0, 0, 5], [4, 3, -1, 0, -1, 1, 0, 4]]`Thu, 03 Mar 2022 08:20:15 +0100https://ask.sagemath.org/question/61364/how-to-select-a-serie-of-sublists-according-to-an-other-list/?comment=61367#post-id-61367