试试这个:
>>> st = '-p 0 0.6724194 0.4034517 -p 0 0 0.4034517 -p 0 0 0.6724194'
>>> [f.strip() for f in st.split('-p') if f]
['0 0.6724194 0.4034517', '0 0 0.4034517', '0 0 0.6724194']
或者:
>>> [', '.join(f.strip().split()) for f in st.split('-p') if f]
['0, 0.6724194, 0.4034517', '0, 0, 0.4034517', '0, 0, 0.6724194']
或者,您可能只想得到一个浮动列表列表:
>>> [[float(e) for e in f.strip().split()] for f in st.split('-p') if f]
[[0.0, 0.6724194, 0.4034517], [0.0, 0.0, 0.4034517], [0.0, 0.0, 0.6724194]]
或者,也许是一本字典:
>>> {i:[float(e) for e in f.strip().split()] for i,f in enumerate(st.split('-p')[1:])}
{0: [0.0, 0.6724194, 0.4034517], 1: [0.0, 0.0, 0.4034517], 2: [0.0, 0.0, 0.6724194]}
或者,如果你真的想要一个正则表达式:
>>> re.findall(r'-[a-zA-Z]\s(\d?\.?\d+\s\d?\.?\d+\s\d?\.?\d+)', st)
['0 0.6724194 0.4034517', '0 0 0.4034517', '0 0 0.6724194']