Re: [問題] 讀取檔案 字串存取問題

看板Python作者 (Apua)時間10年前 (2014/05/11 22:29), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《lingze (walkman)》之銘言: : for i in open('file.txt', 'r'): : print(i,end == '') : 0 152 0 654 482 0 354 612 952 111 : 0 325 130 578 254 320 0 268 921 658 : 296 777 325 0 : ... : 但是讀出來的檔案我想要儲存起來 : 想儲存成這樣: : Matrix[0][0]=0 : Matrix[1][0]=152 : Matrix[1][1]=0 : Matrix[2][0]=654 : Matrix[2][1]=482 : Matrix[2][2]=0 s = open('file.txt').read() L = list(map(int, s.split())) I = filter(lambda i:L[i]==0, range(len(L))) M = [L[a+1:b+1] for a,b in zip([-1]+I,I)] # Matrix if __name__=='__main__': from pprint import pprint pprint(M) ''' [[0], [152, 0], [654, 482, 0], [354, 612, 952, 111, 0], [325, 130, 578, 254, 320, 0], [268, 921, 658, 296, 777, 325, 0]] ''' 資料量大時效率可能不會很高, 我只是當作練習, 請參考看看 -- :wq -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.27.47 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1399818548.A.521.html

05/11 23:20, , 1F
感謝大大~我參考一下:)
05/11 23:20, 1F
文章代碼(AID): #1JRuaqKX (Python)
文章代碼(AID): #1JRuaqKX (Python)