DAY 8
0

## Day8 串列中的方法

1、更新列表

``````list = []
list.append('abc')
list.append('def')
print(list)
``````

3、 刪除

``````list = ['abc','def',12,21]
print(list)
del list [2]
print ("After deleting value at index 2 : ")
print (list)
``````

['abc', 'def', 12, 21]
After deleting value at index 2 :
['abc', 'def', 21]
4、 len()：計算list儲存的資料個數

``````list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
print ("First list length : ", len(list1));
print ("Second list length : ", len(list2));
``````

First list length : 3
Second list length : 2
5、 count()：計算某個資料出現在串列的次數

``````List = [123, 'xyz', 'fff', 'abc', 123];
print ("Count for 123 : ", aList.count(123));
print ("Count for fff : ", aList.count('zara'));
Count for 123 :  2
Count for fff :  1
``````

6、 pop()：pop方法是移除最後一個資料

``````list = ['Google', yahoo, 'abc']
list_pop=list.pop(1)
print ("刪除的項目 :", list_pop)
print ("列表現在為 : ", list)
``````

7、 remove()：remove方法是移除指定的資料

``````List = [123, 'xyz', 'fff', 'abc', 'xyz'];
List.remove('xyz');
print ("List : ", List);
List.remove('abc');
print ("List : ", List);
``````

List : [123, 'zara', 'xyz']
List : [123, 'zara', 'xyz']

Python初學者的自學筆記30