Hello Github community.
I have difficulties with Python. I need to create a set of two floating numbers [float(x), float(y)]. eg:
My 3 coordinates to create are i, j, k
Each variable (i, j, k) is a lists containing two values x, y
list = [x, y]
i, j, k = [1, 1], [2, 1], [3, 4]
The numbers above need to be user specified using the input command.
Inside the python console, i would like to type as input:
[1, 1], [2, 1], [3, 4]
And as output:
i = [1, 1]
j = [2, 1]
k = [3, 4]
The idea is to assign values to 3 lists at once.
Sorry I cannot share any script since i have nothing done yet.
Any help is deeply appreciated! Thanks!
You can get pretty close with repeated regular expression matches using
>>> s = '[1, 1], [2, 1], [3, 4]'
>>> re.findall(r'\[(\d+),\s*(\d+)\]', s)
[('1', '1'), ('2', '1'), ('3', '4')]
The regular expression matches one of your points, you’d just have to post-process to get lists of floats instead of tuples of strings. Note that
re.findall() doesn’t guarantee a number of matches.
Though I’d try to avoid making the user type (relatively) complex structures like that. Maybe you could read the points from a file? Python comes with a built-in JSON parser, and if you read a JSON structure like this you’d directly get your list of lists, including data as numbers:
[[1, 1], [2, 1], [3, 4]]