Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a set of coordinates for 2 points (0 and 1) in list `data`:

data: [0 [834.5, 211.5]][1 [941.5, 362.5]]\n
    [0 [834.5, 213.5]][1 [943.0, 362.5]]\n
    [0 [825.0, 206.0]][1 [961.5, 360.0]]\n
    [0 [814.0, 201.0]][1 [973.5, 358.5]]\n
    [0 [813.0, 200.5]][1 [973.5, 357.5]]\n
    [0 [779.0, 184.0]][1 [992.5, 358.0]]\n
    [0 [758.0, 174.0]][1 [993.5, 366.0]]\n
    [0 [758.0, 173.5]][1 [992.5, 366.5]]\n
    [0 [714.5, 160.0]][1 [991.0, 391.0]]\n
    [0 [691.0, 154.5]][1 [1012.0, 372.0]]\n
    [0 [690.5, 154.0]][1 [1012.0, 372.0]]\n
    [0 [648.0, 157.5]][1 [1012.0, 372.0]]\n
    [0 [631.5, 164.0]][1 [1012.0, 372.0]]\n

I need to put the column starting with 834.5 in `head_x` list, 211.5 in `head_y` list, 941.5 in `foot_x`, and 362.5 in `foot_y`. For the first 9 rows, I used this code:


for i in range(4, len(data), 37):
        head_x.append(data[i:i+5])
    
    for i in range(11, len(data), 37):
        head_y.append(data[i: i+5])
    
    for i in range(22, len(data), 37):
        foot_x.append(data[i: i+5])
    
    for i in range(29, len(data), 37):
        foot_y.append(data[i: i+5])


However, I am not sure how to deal with numbers like 1012.0. I have tried to write some code that would change the for-loop if the `number > 999`, but each character is an element in the overall large list, so that does not work. I am not really sure how to approach this, so some help would be greatly appreciated.

This was the unfortunate output for `head_x`:

['834.5', '834.5', '825.0', '814.0', '813.0', '779.0', '758.0', '758.0', '714.5', '691.0', '[690.', ' [648', '0 [63', '[0 [6', '\n[0 [', ']\n[0 ', ']]\n[0', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]\n[', '0]]']


What I have tried:

I have tried to write some code that would change the for-loop if the `number > 999`, but each character is an element in the overall large list, so that does not work. I am not really sure how to approach this, so some help would be greatly appreciated.
Posted
Updated 10-Jul-22 15:59pm

1 solution

Your code is blindly reading input with an exact format, which is bad, because any data that do not match the format will make your code fail.
Your input with variable format is typical candidate for pattern matching (RegEx, Regular Expression)
This will match the four interesting values in each line
This pattern "(\d+\.\d)" match 1 or more digits followed by a point and a single digit.
And this one match the 4 numbers you want in a line:
\[(\d+\.\d), (\d+\.\d)\].+\[(\d+\.\d), (\d+\.\d)\]

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v2
Comments
CPallini 11-Jul-22 2:14am    
5.
Patrice T 11-Jul-22 2:44am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900