Python課程學習筆記——列表操作

#1、創建列表
同一個列表中可以存放任意基本數據類型
list1=[];
list2=[1,1.1,‘1.1.2’,1+2j];
list3=[1,2,3,4];
list4=[“a”,“b”,“c”,“d”];

#2、通過索引下標訪問列表元素
一次訪問一個元素
print(“list2[2]:”,list2[2]);
print(“list3[2]:”,list3[2]);
print(“list4[2]:”,list4[2]);

print("")

#3、指定範圍 一次訪問多個元素
注意左閉右開,右邊索引不包含
print(“list2[0,2]:”,list2[0:2]);#這裏用的是0:2 不是0,2

print("")

#4、更改元素
print(“test change element:”)
print(“before list4:”,list4);
list4[1] = ‘change’
print(“after list4:”,list4);
list4[1] = ‘b’

print("")

#5、刪除元素 del remove(obj) clear()清空列表
print(“test del element:”)
print(“before list4:”,list4);
del list4[1];
print(“after list4:”,list4);

list4.remove(“c”);
print(“remove after list4:”,list4);
list4.clear();
print(“clear after list4:”,list4);

print("")

#6、向列表中添加元素
append添加在末尾 insert(index,object) 指定索引下標插入元素
print(“test add element:”)
list4.append(“a”);
list4.append(“c”);
print(“append after list4:”,list4);
list4.insert(1, “b”);
list4.insert(3,“d”);
print(“insert after list4:”,list4);

#7、其他操作
print(“列表長度即元素個數len(list4):”,len(list4));

注意:
print("before max list4:",list4);
list4.append(1);
print("列表最大元素max(list4):",max(list4));

執行結果:
before max list4: ['a', 'b', 'c', 'd']
Traceback (most recent call last):
  File "D:\Epan\selfstudy\pythonStudy\helloPython\helloPython\ListPython.py", line 62, in <module>
    print("列表最大元素max(list4):",max(list4));
TypeError: '>' not supported between instances of 'int' and 'str'

print(“列表最大元素max(list4):”,max(list4));

print(“列表最小元素min(list4):”,min(list4));

print(“元素出現的次數count(a):”,list4.count(“a”));

print(“元素在列表中第一次出現的索引list4.index©:”,list4.index(“c”));

注意:
print("before index:",list4);
print("元素在列表中第一次出現的索引list4.index(cd):",list4.index("cd"));
before index: ['a', 'b', 'c', 'd']
Traceback (most recent call last):
  File "D:\Epan\selfstudy\pythonStudy\helloPython\helloPython\ListPython.py", line 74, in <module>
    print("元素在列表中第一次出現的索引list4.index(cd):",list4.index("cd"));
ValueError: 'cd' is not in list

print(“複製列表list4.copy():”,list4.copy());

#print(“反轉列表list4.reverse():”,list4.reverse());這個寫法打印結果爲None

list4.reverse()
print(“反轉列表list4.reverse():”,list4);

print("")
print("")

#8、列表拼接 +即可
print(“before list3:”,list3);
print(“beforelist4:”,list4);
print(“after list3+list4:”,list3 + list4);

#9、列表乘法
相當於列表複製幾遍 並不是列表中的每個元素乘法運算
print("after list42:",list42);

#10、判斷元素是否存在列表中
print(“c in list4?”,‘c’ in list4);

#11、列表嵌套 相當於新建一個列表,元素爲指定的要嵌套的列表
list6 = [list3,list4];
print(“列表嵌套[list3,list4]:”,list6);

#12、迭代

for element  in list4:
    print(element )

迭代寫法特別注意:
(1)list4後面的冒號:不能少,否則報下面的錯
在這裏插入圖片描述
(2)打印元素的語句:print(element) 必須縮進,否則報下面的錯誤:
在這裏插入圖片描述
執行結果:
list2[2]: 1.1.2
list3[2]: 3
list4[2]: c

list2[0,2]: [1, 1.1]

test change element:
before list4: [‘a’, ‘b’, ‘c’, ‘d’]
after list4: [‘a’, ‘change’, ‘c’, ‘d’]

test del element:
before list4: [‘a’, ‘b’, ‘c’, ‘d’]
after list4: [‘a’, ‘c’, ‘d’]
remove after list4: [‘a’, ‘d’]
clear after list4: []

test add element:
append after list4: [‘a’, ‘c’]
insert after list4: [‘a’, ‘b’, ‘c’, ‘d’]
列表長度即元素個數len(list4): 4
列表最大元素max(list4): d
列表最小元素min(list4): a
元素出現的次數count(a): 1
元素在列表中第一次出現的索引list4.index©: 2
複製列表list4.copy(): [‘a’, ‘b’, ‘c’, ‘d’]
反轉列表list4.reverse(): None
反轉列表list4.reverse(): [‘a’, ‘b’, ‘c’, ‘d’]

before list3: [1, 2, 3, 4]
beforelist4: [‘a’, ‘b’, ‘c’, ‘d’]
after list3+list4: [1, 2, 3, 4, ‘a’, ‘b’, ‘c’, ‘d’]
after list4*2: [‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘b’, ‘c’, ‘d’]
c in list4? True
列表嵌套[list3,list4]: [[1, 2, 3, 4], [‘a’, ‘b’, ‘c’, ‘d’]]
迭代list4元素:
a
b
c
d

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章