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, 10 Jan 2017 15:11:34 +0100convert string to listhttps://ask.sagemath.org/question/36154/convert-string-to-list/ I have a string of the form, say, '[(1,2),(2,3)]' and want to obtain the list [(1,2),(2,3)] instead.Tue, 03 Jan 2017 23:09:51 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/Answer by Sillaw for <p>I have a string of the form, say, '[(1,2),(2,3)]' and want to obtain the list [(1,2),(2,3)] instead.</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?answer=36157#post-id-36157Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!Wed, 04 Jan 2017 01:10:25 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?answer=36157#post-id-36157Comment by tmonteil for <p>Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36158#post-id-36158It works for me when attaching a .sage file with the definition of s. Could you please provide your code ?Wed, 04 Jan 2017 01:21:55 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36158#post-id-36158Comment by Sillaw for <p>Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36161#post-id-36161Yes, the code looks like this:
my file = open(DATA + 'test444.sage')
te=myfile.read()
te
'[(1,2),(2,3),(3,4),(3,5)(3,6),(3,7)]'Wed, 04 Jan 2017 03:01:38 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36161#post-id-36161Comment by tmonteil for <p>Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36176#post-id-36176You missed a comma between `(3,5)` and `(3,6)`.Wed, 04 Jan 2017 16:28:36 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36176#post-id-36176Comment by Sillaw for <p>Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36190#post-id-36190THANK YOU!!!Wed, 04 Jan 2017 23:46:42 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36190#post-id-36190Answer by tmonteil for <p>I have a string of the form, say, '[(1,2),(2,3)]' and want to obtain the list [(1,2),(2,3)] instead.</p>
https://ask.sagemath.org/question/36154/convert-string-to-list/?answer=36156#post-id-36156You can use the `eval` Python function:
sage: s='[(1,2),(2,3)]' ; s
'[(1,2),(2,3)]'
sage: type(s)
<type 'str'>
sage: L = eval(s) ; L
[(1, 2), (2, 3)]
sage: type(L)
<type 'list'>
Note however that the string is not preparsed, so you will get Python ints, note Sage integers:
sage: type(L[0][0])
<type 'int'>
To "benefit" from Sage preparsing, you can do:
sage: L = eval(preparse(s)) ; L
[(1, 2), (2, 3)]
sage: type(L[0][0])
<type 'sage.rings.integer.Integer'>Wed, 04 Jan 2017 00:50:00 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?answer=36156#post-id-36156Comment by Sébastien for <p>You can use the <code>eval</code> Python function:</p>
<pre><code>sage: s='[(1,2),(2,3)]' ; s
'[(1,2),(2,3)]'
sage: type(s)
<type 'str'>
sage: L = eval(s) ; L
[(1, 2), (2, 3)]
sage: type(L)
<type 'list'>
</code></pre>
<p>Note however that the string is not preparsed, so you will get Python ints, note Sage integers:</p>
<pre><code>sage: type(L[0][0])
<type 'int'>
</code></pre>
<p>To "benefit" from Sage preparsing, you can do:</p>
<pre><code>sage: L = eval(preparse(s)) ; L
[(1, 2), (2, 3)]
sage: type(L[0][0])
<type 'sage.rings.integer.Integer'>
</code></pre>
https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36253#post-id-36253You may also use the `sage_eval` function which does `eval(preparse(...))`Tue, 10 Jan 2017 15:11:34 +0100https://ask.sagemath.org/question/36154/convert-string-to-list/?comment=36253#post-id-36253