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, 06 Aug 2020 18:53:18 +0200How to drop 'NAN' values from a listhttps://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/I rewrite my question : I have the followinbg list `z=[-1, 0, 1, 2, 3, 'Toto']` and I want to create a list composqe of strictly positive numbers (whichever type they are) and without `toto`Thu, 06 Aug 2020 13:33:51 +0200https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/Answer by slelievre for <p>I rewrite my question : I have the followinbg list <code>z=[-1, 0, 1, 2, 3, 'Toto']</code> and I want to create a list composqe of strictly positive numbers (whichever type they are) and without <code>toto</code></p>
https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?answer=52896#post-id-52896The question is about filtering a list, keeping only those
elements in the list that correspond to positive numbers.
This means filtering out all elements that are not positive numbers.
In particular all strings must be filtered out.
If the list is known to consist only of numbers and 'NAN',
or only of numbers and any strings, it is easy to filter out strings.
To exclude values that are equal to the string 'NAN':
zz = [x for x in z if x != 'NAN' and x > 0]
To exclude all strings:
zz = [x for x in z if not isinstance(x, str) and x > 0]
If list elements can be anything, first check which elements
are real numbers, and test positivity for those.
import numbers
zz = [x for x in z if isinstance(x, numbers.Real) and x > 0]Thu, 06 Aug 2020 14:16:21 +0200https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?answer=52896#post-id-52896Comment by Cyrille for <p>The question is about filtering a list, keeping only those
elements in the list that correspond to positive numbers.</p>
<p>This means filtering out all elements that are not positive numbers.</p>
<p>In particular all strings must be filtered out.</p>
<p>If the list is known to consist only of numbers and 'NAN',
or only of numbers and any strings, it is easy to filter out strings.</p>
<p>To exclude values that are equal to the string 'NAN':</p>
<pre><code> zz = [x for x in z if x != 'NAN' and x > 0]
</code></pre>
<p>To exclude all strings:</p>
<pre><code>zz = [x for x in z if not isinstance(x, str) and x > 0]
</code></pre>
<p>If list elements can be anything, first check which elements
are real numbers, and test positivity for those.</p>
<pre><code>import numbers
zz = [x for x in z if isinstance(x, numbers.Real) and x > 0]
</code></pre>
https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?comment=52897#post-id-52897I rewrite my questionThu, 06 Aug 2020 14:43:48 +0200https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?comment=52897#post-id-52897Comment by Cyrille for <p>The question is about filtering a list, keeping only those
elements in the list that correspond to positive numbers.</p>
<p>This means filtering out all elements that are not positive numbers.</p>
<p>In particular all strings must be filtered out.</p>
<p>If the list is known to consist only of numbers and 'NAN',
or only of numbers and any strings, it is easy to filter out strings.</p>
<p>To exclude values that are equal to the string 'NAN':</p>
<pre><code> zz = [x for x in z if x != 'NAN' and x > 0]
</code></pre>
<p>To exclude all strings:</p>
<pre><code>zz = [x for x in z if not isinstance(x, str) and x > 0]
</code></pre>
<p>If list elements can be anything, first check which elements
are real numbers, and test positivity for those.</p>
<pre><code>import numbers
zz = [x for x in z if isinstance(x, numbers.Real) and x > 0]
</code></pre>
https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?comment=52899#post-id-52899Thanks it's exactly what I was searching but on the way I have had an other idea to obtain what I was searching and I have encountered a new difficulty so I am going to ask a new question. Sorry and 1000000 thanks for your help.Thu, 06 Aug 2020 18:53:18 +0200https://ask.sagemath.org/question/52890/how-to-drop-nan-values-from-a-list/?comment=52899#post-id-52899