Re: [問題] list中插入資料效能的問題

看板Python作者 (Zola)時間7年前 (2017/02/21 17:00), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
我不確定是不是你要的: from time import time old = [str(i) for i in range(10**7)] # set up a list insert = ['n'] # set up an insert list def test(old, insert): t0 = time() add = (old[i : i + 1024] + insert for i in range(0, len(old), 1024)) new = [] for i in range(0, len(old), 1024): new += next(add) print(time() - t0) return new new = test(old, insert) 輸出結果: 0.2487800121307373 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.73.157.3 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1487667606.A.6B4.html
文章代碼(AID): #1Oh06MQq (Python)
文章代碼(AID): #1Oh06MQq (Python)