或者,您可以只使用字符串操作,在許多情況下,它比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)]
-1:你顯然沒有在所有進行測試。 –
其實我只是在發佈前測試它 - 這裏是從我的shell窗口粘貼輸出: 馬克 - Cohens的iMac%蟒蛇Ÿ 312(21.1) 312 21.1 工作正常,我關於Python 2.7.3。你爲什麼說我沒有測試它? –
你的輸入文件在一行中包含多對數字(如在問題中)? –