列表是python開發過程當中最經常使用的數據類型之一,列表俗稱:list ,特色以下:python
1.列表由一個或者多個數據構成,數據的類型能夠不相同也能夠相同;git
2.列表中的數據須要寫在[]中括號內部,數據與數據之間用逗號隔開;github
3.列表是一個有序的集合,下標索引默認重 0 開始,和字符串相似;微信
具體代碼示例以下:app
1ide 2函數 3spa 4code 5orm 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(我的博客地址): shuopython.com @WeChat Official Account(微信公衆號):猿說python @Github:www.github.com
@File:python_list.py @Time:2019/9/25 20:45
@Motto:不積跬步無以致千里,不積小流無以成江海,程序人生的精彩須要堅持不懈地積累! """
list1 = list() #定義一個空列表 print("list1 :",list1) list2 = [1,2,3,4] #定義一個整數類型的列表賦值給list2 print("list2 : %s" % list2) list3 = ["a","b","c"] #定義一個字符串類型的列表賦值給list3 print("list3 : {}" .format(list3)) list4 = [0,"hello",True] #定義一個不一樣數據類型的列表賦值給list4 print("list4 : {0}" .format(list4)) |
輸出結果:
1 2 3 4 |
list1 : [] list2 : [1, 2, 3, 4] list3 : ['a', 'b', 'c'] list4 : [0, 'hello', True] |
在python開發過程,list列表最經常使用的就是增刪改查,下面跟上代碼一一講解:
通常可使用append()函數來爲列表list添加數據,默認將數據追加在末尾。示例代碼以下:
1 2 3 4 5 |
list1 = list() #定義一個空列表 print("list1 : ",list1) list1.append("hello") # 在列表list的末尾添加字符串 'hello' list1.append(True) # 在列表list的末尾添加布爾值 True print("list1 : ",list1) |
輸出結果:
1 2 |
list1 : [] list1 : ['hello', True] |
列表中的數據從左到右,索引值默認重0 開始以此遞增,和字符串的索引值相似,刪除使用 del 關鍵字,直接列表List時根據數據對應的索引值直接刪除便可,代碼以下:
1 2 3 4 5 6 7 8 |
list2 = [1,2,3,4,5,6,7,False,"python"] print("刪除數據以前:{}".format(list2)) del list2[0] # 刪除列表中的(索引值等於0)第一個數據,此時list2 中數據爲[2,3,4,5,6,7,False,"python"] print("第一次數據以後:{}".format(list2)) del list2[0] # 基於上一次的結果,刪除(索引值等於0)第一個數據,此時list2 中數據爲[3,4,5,6,7,False,"python"] print("第二次數據以後:{}".format(list2)) del list2[3] # 基於上一次的結果,刪除(索引值等於3)第四個數據,此時list2 中數據爲[3,4,5,7,False,"python"] print("第三次數據以後:{}".format(list2)) |
輸出結果:
1 2 3 4 |
刪除數據以前:[1, 2, 3, 4, 5, 6, 7, False, 'python'] 第一次數據以後:[2, 3, 4, 5, 6, 7, False, 'python'] 第二次數據以後:[3, 4, 5, 6, 7, False, 'python'] 第三次數據以後:[3, 4, 5, 7, False, 'python'] |
直接根據索引值找到列表中對應的數據,而後賦值便可。
1 2 3 4 5 6 |
list2 = [1,2,3,4,5,6,7,False,"python"] print("修改數據以前:{}".format(list2)) list2[2] = False # 修改列表索引值爲2的數據(即列表中的第三個數據),直接賦值爲bool變量 False print("第一次修改數據以後:{}".format(list2)) list2[0] = "python" # 修改列表索引值爲0的數據(即列表中的第第一個數據),直接賦值爲bool變量 False print("第二次修改數據以後:{}".format(list2)) |
輸出結果:
1 2 3 |
修改數據以前:[1, 2, 3, 4, 5, 6, 7, False, 'python'] 第一次修改數據以後:[1, 2, False, 4, 5, 6, 7, False, 'python'] 第二次修改數據以後:['python', 2, False, 4, 5, 6, 7, False, 'python'] |
直接根據索引值找到列表中對應的數據便可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
list2 = [1,2,3,4,5,6,7,False,"python"] print(list2[0]) # 輸出列表中索引值爲0的數據,即第一個元素 print(list2[5]) # 輸出列表中索引值爲5的數據,即第六個元素 print(len(list2)) # 獲取列表中數據個數
# 獲取列表的最後一個元素,注意要 len(list2) - 1,由於最後一個元素的索引值爲8 print("list2中最後一個數據是:",list2[len(list2)-1])
print("***"*20) #小竅門:直接輸出60個* # 遍歷列表 print("遍歷列表方式一:") for i in list2: print(i)
print("***"*20) #小竅門:直接輸出60個* print("遍歷列表方式二:") for i in range(len(list2)): # 內置函數 type()獲取數據類型 print("list2列表中索引值{}對應的數據是{},數據類型是:{}".format(i,list2[i],type(list2[i]))) |
輸出結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
1 6 9 list2中最後一個數據是: python ************************************************************ 遍歷列表方式一: 1 2 3 4 5 6 7 False python ************************************************************ 遍歷列表方式二: list2列表中索引值0對應的數據是1,數據類型是:<class 'int'> list2列表中索引值1對應的數據是2,數據類型是:<class 'int'> list2列表中索引值2對應的數據是3,數據類型是:<class 'int'> list2列表中索引值3對應的數據是4,數據類型是:<class 'int'> list2列表中索引值4對應的數據是5,數據類型是:<class 'int'> list2列表中索引值5對應的數據是6,數據類型是:<class 'int'> list2列表中索引值6對應的數據是7,數據類型是:<class 'int'> list2列表中索引值7對應的數據是False,數據類型是:<class 'bool'> list2列表中索引值8對應的數據是python,數據類型是:<class 'str'> |
注意上面代碼中兩種循環方式的區別,第一種循環是直接根據列表list中的數據經過偏移依次遍歷,第二種是經過列表list的索引值遍歷循環,相似查找操做。順便回憶一下內置函數type()的使用。
列表List截取和字符串的操做相似,直接根據List的下標索引值操做便可,演示代碼以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
list1 = ["猿",True,"python",5.5,"hello",20,"list","study"] print("list1:",list1)
# 截取在列表中索引值爲2-4的數據,注意截取並不包括4 list2 = list1[2:4] print("list2:",list2)
# 截取在列表中索引值爲1-5的數據,注意截取並不包括5 list3 = list1[1:5] print("list3:",list3)
# 截取在列表中索引值爲0-4的數據,冒號前面不設置參數,默認重0開始,注意截取並不包括4 list4 = list1[:4] print("list4:",list4)
# 截取在列表中索引值爲2-末尾的數據,冒號後面不設置參數,默認截取到最後一位數據,注意截取包括最後一位 list5 = list1[2:] print("list5:",list5) |
輸出結果:
1 2 3 4 5 |
list1: ['猿', True, 'python', 5.5, 'hello', 20, 'list', 'study'] list2: ['python', 5.5] list3: [True, 'python', 5.5, 'hello'] list4: ['猿', True, 'python', 5.5] list5: ['python', 5.5, 'hello', 20, 'list', 'study'] |
能夠經過使用sort()函數或者reverse()函數對列表list排序,演示代碼以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 對數字排序 list1 = [10,2,30,4,5,6,7] #定義一個空列表 print("排序以前:",list1) list1.sort() # 默認重小到大依次排序 print("排序以後:%s" % list1) list1.reverse() # 默認重大到小依次排序 print("排序以後:{}".format(list1))
print("***"*20) # 小竅門:直接打印60個* #對字符串排序 list2 = ["f","e","c","a"] print("排序以前:",list2) list2.sort() # 默認重小到大依次排序 print("排序以後:%s" % list2) list2.reverse() # 默認重大到小依次排序 print("排序以後:{}".format(list2)) |
輸出結果:
1 2 3 4 5 6 7 |
排序以前: [10, 2, 30, 4, 5, 6, 7] 排序以後:[2, 4, 5, 6, 7, 10, 30] 排序以後:[30, 10, 7, 6, 5, 4, 2] ************************************************************ 排序以前: ['f', 'e', 'c', 'a'] 排序以後:['a', 'c', 'e', 'f'] 排序以後:['f', 'e', 'c', 'a'] |
使用list(str),強制將str字符串轉爲list列表,演示代碼以下:
1 2 3 4 |
str1 = "hello world" list1 = list(str1) # 強制將str1 字符串轉爲列表 list print("str1:{},數據類型:{}".format(str1,type(str1))) print("list1:{},數據類型:{}".format(list1,type(list1))) |
輸出結果:
1 2 |
str1:hello world,數據類型:<class 'str'> list1:['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],數據類型:<class 'list'> |
使用join()函數將列表直接轉爲字符串,演示代碼以下:
1 2 3 4 |
list2 = ["猿說python",'-',"python教程"] str2 = "".join(list2) print("list2:{},數據類型:{}".format(list2,type(list2))) print("str2:{},數據類型:{}".format(str2,type(str2))) |
輸出結果:
1 2 |
list2:['猿說python', '-', 'python教程'],數據類型:<class 'list'> str2:猿說python-python教程,數據類型:<class 'str'> |
1.對於列表的增刪改查是python開發中常常使用的內容,須要所有掌握.
2.注意列表List與字符串str的寫法區別:
1 2 |
a = "hello world" # 字符串 b = ["hello world"] # 列表,列表中只有一個字符串數據 |
1.Pycharm配置開發模板
2.python for循環
3.python 字符串
轉載請註明:猿說Python » python列表