string to list of numbers conversion
henning
henning.von.rosen at gmail.com
Wed Nov 8 23:10:13 EST 2006
jm.suresh at no.spam.gmail.com skrev:
> Hi,
> I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers.
I think your question is deeper and more natural than is clear from the
many recepies given so far in this thread, so I'll take on another
point of view,
>From a language design perspective, there is no reason why not the
parsing capacity of the Python interpreter would be accessible in a
modular fashion to the user/programmer. E.g used like this:
I an imaginable Python, define you expect for an answer. In this case:
(1)
# import junctions, types from maybefuture:-)
string = ((1,2), (3,4))
type a = tuple a | int
myTuple = eval(string, goal=a)
Obviously, if you expect _only_ the given form, then this might be
better:
(2)
# import types from maybefuture:-)
type a = ((int,int),(int,int))
myTuple = eval(string, goal=a)
Note the use of a "a|b" in line 2 (I think Perl 6 is among the few
programming languages giving a reasonable semantics to junctions so
far).
Version 2 above sholud not be a big addition to Python conceptually.
Motivation:
It is easy to think clearly about.
It makes it easier to use eval safely and makes code more readable.
This is a topic of interest to me, so feel free to post either on list
or directly to me.
Thanks/Henning
More information about the Python-list
mailing list