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.Wed, 08 Jul 2020 06:42:07 +0200convert a string in float but not a list of string in list of floathttps://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/ This is a very weird behaviour.
Suppose I have a list such
A=['1.2', '2.565', '3.45']
If I I create the command `A[1]=float(A[1])` the result is
A=[1.2, '2.565', '3.45']
Now if I ask
for i in range(len(A)):
A[i]=float(A[i])
the result is `ValueError: could not convert string to float:`
Do I do something forbiden ?Tue, 07 Jul 2020 11:00:26 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/Comment by rburing for <p>This is a very weird behaviour.</p>
<p>Suppose I have a list such</p>
<pre><code>A=['1.2', '2.565', '3.45']
</code></pre>
<p>If I I create the command <code>A[1]=float(A[1])</code> the result is</p>
<pre><code>A=[1.2, '2.565', '3.45']
</code></pre>
<p>Now if I ask</p>
<pre><code>for i in range(len(A)):
A[i]=float(A[i])
</code></pre>
<p>the result is <code>ValueError: could not convert string to float:</code></p>
<p>Do I do something forbiden ?</p>
https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52368#post-id-52368Please write the complete error message. What comes next is quite crucial. Probably your actual `A` contains strings with quotes or other extra characters (unlike the `A` you wrote here, which is fine and does not cause any error). Or if that is really the complete error message, then your `A` contains the empty string or a string consisting only of whitespace.Tue, 07 Jul 2020 11:19:16 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52368#post-id-52368Comment by Cyrille for <p>This is a very weird behaviour.</p>
<p>Suppose I have a list such</p>
<pre><code>A=['1.2', '2.565', '3.45']
</code></pre>
<p>If I I create the command <code>A[1]=float(A[1])</code> the result is</p>
<pre><code>A=[1.2, '2.565', '3.45']
</code></pre>
<p>Now if I ask</p>
<pre><code>for i in range(len(A)):
A[i]=float(A[i])
</code></pre>
<p>the result is <code>ValueError: could not convert string to float:</code></p>
<p>Do I do something forbiden ?</p>
https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52370#post-id-52370rburing ok I have verified there are null string on my data base. Do you know how eliminate them ?Tue, 07 Jul 2020 17:36:15 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52370#post-id-52370Comment by John Palmieri for <p>This is a very weird behaviour.</p>
<p>Suppose I have a list such</p>
<pre><code>A=['1.2', '2.565', '3.45']
</code></pre>
<p>If I I create the command <code>A[1]=float(A[1])</code> the result is</p>
<pre><code>A=[1.2, '2.565', '3.45']
</code></pre>
<p>Now if I ask</p>
<pre><code>for i in range(len(A)):
A[i]=float(A[i])
</code></pre>
<p>the result is <code>ValueError: could not convert string to float:</code></p>
<p>Do I do something forbiden ?</p>
https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52372#post-id-52372What does this question have to do with Sage? Isn't is just a plain Python question?Tue, 07 Jul 2020 18:25:02 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52372#post-id-52372Comment by Cyrille for <p>This is a very weird behaviour.</p>
<p>Suppose I have a list such</p>
<pre><code>A=['1.2', '2.565', '3.45']
</code></pre>
<p>If I I create the command <code>A[1]=float(A[1])</code> the result is</p>
<pre><code>A=[1.2, '2.565', '3.45']
</code></pre>
<p>Now if I ask</p>
<pre><code>for i in range(len(A)):
A[i]=float(A[i])
</code></pre>
<p>the result is <code>ValueError: could not convert string to float:</code></p>
<p>Do I do something forbiden ?</p>
https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52373#post-id-52373Perhaps you are true, but it happens in a Sage session. From the user point of view it is often difficult to know if it is a Sage problem or a Python one. I am an economist not a computer scientist. And, more than that, rburing was true, it was not a code problem but a data problem. I want use data but I also want that the link between data and theory be the most explicit. It's the reason why I have chosen Sage.Wed, 08 Jul 2020 06:42:07 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?comment=52373#post-id-52373Answer by Sébastien for <p>This is a very weird behaviour.</p>
<p>Suppose I have a list such</p>
<pre><code>A=['1.2', '2.565', '3.45']
</code></pre>
<p>If I I create the command <code>A[1]=float(A[1])</code> the result is</p>
<pre><code>A=[1.2, '2.565', '3.45']
</code></pre>
<p>Now if I ask</p>
<pre><code>for i in range(len(A)):
A[i]=float(A[i])
</code></pre>
<p>the result is <code>ValueError: could not convert string to float:</code></p>
<p>Do I do something forbiden ?</p>
https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?answer=52369#post-id-52369This works:
sage: A = ['1.2', '2.565', '3.45']
sage: A = [float(a) for a in A]
sage: A
[1.2, 2.565, 3.45]Tue, 07 Jul 2020 14:31:25 +0200https://ask.sagemath.org/question/52367/convert-a-string-in-float-but-not-a-list-of-string-in-list-of-float/?answer=52369#post-id-52369