I have a long list like this:
some_list=["PointID:","1","'Y':", "1.134","'X':","1.223", "'Z':","1.0","PointID","2", "'Y':","1.4", "'X':","1.75","'Z':","1.0"]
I need to extract the coordinates and put them in txt file, which will be use as input for CAD Software drawing. My solution is:
some_list=["PointID:","1","'Y':", "1.134","'X':","1.2223", "'Z':","1.0","PointID","2", "'Y':","1.4", "'X':","1.75","'Z':","1.0"]
for index, element in enumerate(some_list):
if element == "'X':" or element == "'Y':" or element == "'Z':":
print(index, next(element))
The output is:
2 'Y':
4 'X':
6 'Z':
10 'Y':
12 'X':
14 'Z':
But actually, I am interested in the coordinates 1.134, 1.223 etc.
Something like this:
Y: 1.134
X: 1.223
etc I try print(element 1) and print(next(element)) which of course do not work because. Next works with iterators, not iterable. Any suggestions on how I could extrude the coordinates from the list? I will really appreciate any help.
CodePudding user response:
Not sure what is the expected output, but here is a generator that yields the X/Y/Z and the following points:
some_list=["PointID:","1","'Y':", "1.134","'X':","1.223", "'Z':","1.0","PointID","2", "'Y':","1.4", "'X':","1.75","'Z':","1.0"]
def get_coord(l):
it = iter(l)
for item in it:
if item in ["'X':", "'Y':", "'Z':"]:
item = item.replace("'", '')
yield f"{item} {next(it, None)}"
print('\n'.join(get_coord(some_list)))
output:
Y: 1.134
X: 1.223
Z: 1.0
Y: 1.4
X: 1.75
Z: 1.0