小題3 列表複製

2021-08-14 09:20:58 字數 1506 閱讀 6863

乙個列表複製100分怎麼複製的快:

import time

import copy

defrun1

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

list(a)

end = time.clock()

print('list方法耗時:%f' % (end - start))

defrun2

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

copy.copy(a)

end = time.clock()

print("copy.copy耗時:%f" % (end - start))

defrun3

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

copy.deepcopy(a)

end = time.clock()

print("copy.deepcopy耗時:%f" % (end - start))

defrun4

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

b_list =

end = time.clock()

print("迴圈新增耗時:%f" % (end - start))

defrun5

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

b_list =

b_list = a[:]

end = time.clock()

print("a[:]複製耗時:%f" % (end - start))

defrun6

(): a = [1, 2, 3]

start = time.clock()

for i in range(100):

b_list =

b_list = a

end = time.clock()

print("引用複製耗時:%f" % (end - start))

if __name__ == '__main__':

run1()

run2()

run3()

run4()

run5()

run6()

結果

copy.copy耗時:0.000066

copy.deepcopy耗時:0.000499

迴圈新增耗時:0.000056

a[:]複製耗時:0.000016

引用複製耗時:0.000004

Python筆記3 列表

列表的簡單操作 列表操作 列表組合 list4 1,2,3 list5 4,5,6 print list4,list5 print list4 list5 列表重複 list6 7,8,9 print list6 3 in not in print 1 in 1,2,3 列表擷取 切片 list7 ...

Python基礎3 列表

while迴圈控制語句 break 和continue是在迴圈中使 的關鍵字 break 出現在迴圈體中,終止迴圈 continue 出現在迴圈體中,跳過當次迴圈,執行下次迴圈while 迴圈巢狀 語法 while 條件語句1 條件1滿足條件時執行的 塊 while 條件語句2 條件2滿足條件時執行...

多個列表append python3 列表

insert插入,索引 新增的位置 資料 新增的內容 list1.insert 0,0 extend 可以將兩個列表進行合併 list1.extend list2 del 精準刪除,索引 del list1 4 pop 預設刪除最後乙個資料,也可以精準刪除 list1.pop orlist1.pop...