tt = 'hello' #定義一個包含多個類型的 list list1 = [1,4,tt,3.4,"yes",[1,2]] print(list1,id(list1)) print("1.----------------") #比較 list 中添加元素的幾種方法的用法和區別 list3 = [6,7] l2 = list1 + list3 print(l2,id(l2)) print("2.----------------") l2 = list1.extend(list3) print(l2,id(l2)) print(list1,id(list1)) print("3.----------------") l2 = list1.append(list3) print(l2,id(l2)) print(list1,id(list1))輸出結果爲:
[1, 4, 'hello', 3.4, 'yes', [1, 2]] 2251638471496
1.----------------
[1, 4, 'hello', 3.4, 'yes', [1, 2], 6, 7] 2251645237064
2.----------------
None 1792287952
[1, 4, 'hello', 3.4, 'yes', [1, 2], 6, 7] 2251638471496
3.----------------
None 1792287952
[1, 4, 'hello', 3.4, 'yes', [1, 2], 6, 7, [6, 7]] 2251638471496python
tt = 'hello' #定義一個包含多個類型的 list list1 = [1,4,tt,3.4,"yes",[1,2]] print(list1) del list1[2:5] print(list1) del list1[2] print(list1)輸出結果爲:
[1, 4, 'hello', 3.4, 'yes', [1, 2]]
[1, 4, [1, 2]]
[1, 4]編程
tt = 'hello' #定義一個包含多個類型的 list list1 = [1,4,tt,3.4,"yes",[1,2]] l2 = list1 print(id(l2),id(list1)) del list1 print(l2) print(list1)運行結果以下:
1765451922248 1765451922248
[1, 4, 'hello', 3.4, 'yes', [1, 2]]
Traceback (most recent call last):
File "C:\Users\mengma\Desktop\demo.py", line 8, in <module>
print(list1)
NameError: name 'list1' is not definedapp
tt = 'hello' #定義一個包含多個類型的 list list1 = [1,4,tt,3.4,"yes",[1,2]] l2 = list1 l3 = l2 del l2[:] print(l2) print(l3)輸出結果爲:
[]
[]函數
#引入gc庫 import gc tt = 'hello' #定義一個包含多個類型的 list list1 = [1,4,tt,3.4,"yes",[1,2]] del list1 #回收內存地址 gc.collect()