※ 引述《darklimit ()》之銘言:
: 有一串檔案可能中間夾帶著
: able#1 unable#1
: 應用split讓字與數值分開
: 就會變成
: ['able','1']
: ['unable','1']
: 但在檔案的部分可能有包含這樣的字串
: voluminous#3 copious#1
: 如果應用split('#')來寫
: 這一串就會變成
: ['voluminous', '3 copious','1']
: 結果就會變成這樣
: 可是我是要讓結果也變成
: ['voluminous',3]
: ['cioious,1']
: 那我應該怎麼寫?
[x.split("#") for x in "voluminous#3 copious#1".split(" ")]
其實只需要一行 但推文擠不下所以用回的...
如果要寫成函數
def string_split_twice(word,first,second):
return [x.split(second) for x in word.split(first)]
回傳的是個 list ,如果要換別的資料型態就傳回來再另外處理吧
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.117.174