python - How to parse tuple data in one line -
i have take user input in 1 line in form:
c = (39.447; 94.657; 11.824) n = (39.292; 95.716; 11.027) ca = (39.462; 97.101; 11.465)
and have translate have 3 variables each corresponding appropriate tuple. complicated semi colons , fact has on 1 line.
this have far i'm having trouble parsing 1 line, thought eval() might work because input resembles variable assignment "syntaxerror can't assign literal". feel there should simple way this.
class tupleclean(str): def clean(self): new_list = [] clean_coord = self.strip("() ") split_coord = clean_coord.split(";") in split_coord: new_list.append(float(i)) tuple_coord = (new_list[0], new_list[1], new_list[2]) return(tuple_coord) coord_1 = input("input coordinates carbon in format (p; q; r): ") coord_2 = input("input coordinates nitrogen in format (p; q; r): ") coord_3 = input("input coordinates calcium in format (p; q; r): ") coord_1_clean = tupleclean(coord_1) coord_1_clean = coord_1_clean.clean() coord_2_clean = tupleclean(coord_2) coord_2_clean = coord_2_clean.clean() coord_3_clean = tupleclean(coord_3) coord_3_clean = coord_3_clean.clean()
the constructor of tuple
class can take iterable argument:
class tupleclean(str): def clean(self): new_list = [] clean_coord = self.strip("() ") return tuple(map(float, clean_coord.split(";")))
(map applies function float
each item of list clean_coord.split(";")
)