または、多くの場合、RE よりも単純で高速な文字列操作のみを使用することもできます。
>>> s = "312(21.1) 378(25.5) 374(25.3) 157(10.6) 260(17.6) 1481(100) 125(28.1) 91(20.4) 94(21.1) 52(11.7) 83(18.7) 445(100) 50(28.4) 44(25) 29(16.5) 12(6.8) 41(23.3) 176(100)"
それをトークンに分割します
>>> tokens = s.split()
>>> tokens
['312(21.1)', '378(25.5)', '374(25.3)', '157(10.6)', '260(17.6)', '1481(100)', '125(28.1)', '91(20.4)', '94(21.1)', '52(11.7)', '83(18.7)', '445(100)', '50(28.4)', '44(25)', '29(16.5)', '12(6.8)', '41(23.3)', '176(100)']
最後に「)」を削除します
>>> intermediary1 = [ entry[:-1] for entry in tokens ]
>>> intermediary1
['312(21.1', '378(25.5', '374(25.3', '157(10.6', '260(17.6', '1481(100', '125(28.1', '91(20.4', '94(21.1', '52(11.7', '83(18.7', '445(100', '50(28.4', '44(25', '29(16.5', '12(6.8', '41(23.3', '176(100']
2つの文字列に分かれます
>>> intermediary2 = [ entry.split('(') for entry in intermediary1 ]
>>> intermediary2
[['312', '21.1'], ['378', '25.5'], ['374', '25.3'], ['157', '10.6'], ['260', '17.6'], ['1481', '100'], ['125', '28.1'], ['91', '20.4'], ['94', '21.1'], ['52', '11.7'], ['83', '18.7'], ['445', '100'], ['50', '28.4'], ['44', '25'], ['29', '16.5'], ['12', '6.8'], ['41', '23.3'], ['176', '100']]
数値に変換 (整数、浮動小数点)
>>> numbers = [ ( int(num1), float(num2) ) for num1, num2 in intermediary2 ]
>>> numbers
[(312, 21.1), (378, 25.5), (374, 25.3), (157, 10.6), (260, 17.6), (1481, 100.0), (125, 28.1), (91, 20.4), (94, 21.1), (52, 11.7), (83, 18.7), (445, 100.0), (50, 28.4), (44, 25.0), (29, 16.5), (12, 6.8), (41, 23.3), (176, 100.0)]
または、リスト内包表記を使用してより短い方法で:
>>> tokens = [ entry[:-1].split('(') for entry in s.split()]
>>> numbers = [ ( int(num1), float(num2) ) for num1, num2 in tokens ]
>>> numbers
[(312, 21.1), (378, 25.5), (374, 25.3), (157, 10.6), (260, 17.6), (1481, 100.0), (125, 28.1), (91, 20.4), (94, 21.1), (52, 11.7), (83, 18.7), (445, 100.0), (50, 28.4), (44, 25.0), (29, 16.5), (12, 6.8), (41, 23.3), (176, 100.0)]