列表的一些基本操作
List_1 = []
定義包含數據的list
List_2 = [1,2.0,3+4j,”abc”,(1,2,3),[5,6],{“username”:”hhq”,”password”:”123456”},{9,8,7}]
List生成一個列表
>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> list("abcd")
[‘a‘, ‘b‘, ‘c‘, ‘d‘]
增加元素
append 在列表末尾追加元素
>> List_2 = [1,2.0,3+4j,"abc",(1,2,3),[5,6],{"username":"hhq","password":"123456"},{9,8,7}]>> List_2.append("XYZ")
>> List_2
[1, 2.0, (3+4j), ‘abc‘, (1, 2, 3), [5, 6], {‘username‘: ‘hhq‘, ‘password‘: ‘123456‘}, {8, 9, 7}, ‘XY
Z‘]
查看list類型
>> type(List_2)
<class ‘list‘>
Insert(index,item)在指定位置插入元素
>> List_2.insert(0,"400")
>> List_2
[‘400‘, 1, 2.0, (3+4j), ‘abc‘, (1, 2, 3), [5, 6], {‘username‘: ‘hhq‘, ‘password‘: ‘123456‘}, {8, 9,7}, ‘XYZ‘]
a.extend(b) 把b的每個元素追加到列表a
>> a =[1,2,3]
>> b=["a","b","c"]
追加列表
>> a.extend(b)
>> a
[1, 2, 3, ‘a‘, ‘b‘, ‘c‘]
追加字符串
>> a.extend(("xyz"))
>> a
[1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘x‘, ‘y‘, ‘z‘]
追加元組
>> a.extend((4,5,6))
>> a[1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘x‘, ‘y‘, ‘z‘, 4, 5, 6]
追加集合
>> a.extend({"X","Y"})
>> a
[1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘x‘, ‘y‘, ‘z‘, 4, 5, 6, ‘X‘, ‘Y‘]
追加字典 註意:只會字典的key追加到列表a中
>> a.extend({"username":"hhq","passwd":"123456"})
>> a
[1, 2, 3, ‘a‘, ‘b‘, ‘c‘, ‘x‘, ‘y‘, ‘z‘, 4, 5, 6, ‘X‘, ‘Y‘, ‘username‘, ‘passwd‘]
小練習:
把以下l的a,d,g取出,輸出字符串”adg”
>> l = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
方式1:
>> string = ""
>> for c in l[::3]:
... string += c
...
>> print(string)
adg
方式2:
>> "".join(l[::3])#join把列表轉換為字符串
‘adg‘
更新列表元素:
更新一個元素值
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
>> l[0] = "A"
>> l
[‘A‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
更新連續的多個元素值
>> list_1[:2] = [3,4]
>> list_1
[3, 4, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
**刪除列表元素
刪除單個列表元素
>> result
[‘a‘, ‘b‘, ‘c‘, 2, 3, 4, 5, 6, 7, 8, 9]
>> del result[0]
>> result
[‘b‘, ‘c‘, 2, 3, 4, 5, 6, 7, 8, 9]
刪除連續的多個列表元素
>> del result[0:2]
>> result
[2, 3, 4, 5, 6, 7, 8, 9]
刪除所有列表元素但不刪除列表
>> del result[:]
刪除整個列表
>> del result
pop([item])刪除元素
item可選參數
如果帶item,直接刪除item元素
如果不帶item刪除末尾的一個元素並返回
帶參數
>> l = list(range(10))
>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> item = l.pop()#沒帶參數直接刪除末尾元素
>> item
9
>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8]
指定值
>> l.pop(5)#指定值,直接刪除指定值
5
>> l
[0, 1, 2, 3, 4, 6, 7, 8]
clear()清空列表
>> n.clear()#等價於del n[:]
>> n
[]
remove(item)
刪除指定的列表元素item
>> list_2
[9, 8, 3, 2, 1, 0]
>> list_2.remove(9)
>> list_2
[8, 3, 2, 1, 0]
小練習:
實現一個小的圖書館程序
info = """
add: to add a book the library
lend: to lend a book from
getall: to list all books
.:to exit
"""
print(info)
book_list = []
while True:
command = input("please input the command: add or lend or . exit: ")
if command == "add":
book_name = input("please input add book: ")
book_list.append(book_name)
elif command == "lend":
book_name = input("please input lend book: ")
if book_name in book_list:
book_list.remove(book_name)
else:
print ("not exists")
elif command == "getall":
if book_list:
print("book list:",book_list)
else:
print("no book")
elif command ==".":
Break
其他操作
count(item)計算item在列表中的次數
>> n = list(range(10))
>> n.count(3)
1
Index(item) 求元素在列表中第一次出現的索引位置
>> n.index(2)
2
排序
sorted(list)
不改變原有list的順序
有返回值,返回排序後的列表
>> a = [1,2,3,1,4,9,3]
>> sorted_a = sorted(a)
>> sorted_a
[1, 1, 2, 3, 3, 4, 9]
list.sort()
直接把原列表排序,返回值是None
>> a.sort()
>> a
[1, 1, 2, 3, 3, 4, 9]
>> a
list.reverse()
翻轉元素但不排序
>> a.reverse()
>> a
[9, 4, 3, 3, 2, 1, 1]
sort() sorted()實現降序排序
只需要添加reverse=True即可
>> list_1 = [1,3,4,2,0,9]
>> sorted_l = sorted(list_1,reverse=True)
>> sorted_l
[9, 4, 3, 2, 1, 0]>> list_2 = [3,2,1,0,8,9]
>> list_2.sort(reverse=True)
>> list_2
[9, 8, 3, 2, 1, 0]
訪問列表元素
利用列表索引訪問列表元素
>> list_1 = list(range(10))
>> list_1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0>> list_1[-1]
9>> list_1[0:2]
[0, 1]>> list_1[0:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> list_1[:3]
[0, 1, 2]>> list_1[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> list_1[-3:-1]
[7, 8]
指定步長,訪問偶數索引位置元素
>> list_1[0::2]
[0, 2, 4, 6, 8]
指定步長,訪問奇數索引位置元素
>> list_1[1::2]
[1, 3, 5, 7, 9]
列表的一些基本操作