建立列表 python
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric'] 4 #或 5 a = list(['alex', 'seven', 'eric'])
list轉換列表
"""(轉換成列表,須要轉換的可迭代數據變量) 注意:能轉換成列表的必須是可迭代的,也就是能夠被for循環的"""
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #list將16進制轉換成字符串 4 a = "林貴秀" 5 b = list(a)#將一個字符串轉換成一個列表,只要是能夠被for循環的均可以用list轉換成列表 6 print(b) 7 #輸出 ['林', '貴', '秀']
字符串,元組,列表 > 均可以轉換成列表,轉換成列表都是能夠被for循環的,for循環每次循環到的數據就是列表的一個元素
基本操做:
索引
切片
追加
刪除
長度
循環
包含app
打印列表裏的元素ide
打印出列表裏的元素是以列表變量加元素下標的方式來打印ui
索引this
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric'] 4 print(a[0]) 5 print(a[1]) 6 #輸出 alex seven 這樣就打印出了,第零個和第一個下標的元素
切片spa
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #切片 4 a = ['alex', 'seven', 'eric'] 5 print(a[0:2]) 6 #輸出 alex seven 這樣就切片出了,第零一個和第二個下標的元素
統計列表裏有多少個元素rest
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric'] 4 print(len(a)) 5 #輸出 3 統計列表裏有多少個元素
循環列表code
while循環blog
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #while循環 4 a = ['alex', 'seven', 'eric'] 5 b = 0 6 while b < len(a): #len(統計列表裏的元素) 7 print(a[b]) 8 b += 1 9 #循環出列表裏的全部元素
for循環排序
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #for循環 4 a = ['alex', 'seven', 'eric'] 5 for b in a: #b爲自定義循環變量 6 print(b) 7 #循環出列表裏的全部元素
append(self, p_object)
"""(追加列表)"""要追加的元素
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric'] 4 a.append("linguixiu") 5 print(a) 6 #打印出 ['alex', 'seven', 'eric', 'linguixiu'] 也就是追加了一個元素
count(self, value)
"""(統計元素在列表裏出現的次數)"""要統計的元素
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric', 'seven',] 4 b = a.count("seven") 5 print(b) 6 #打印出 2 統計到seven在列表中出現兩次
extend(self, iterable)
"""(擴展列表)"""要擴展的可迭代變量:可迭代是隻要是能經過for循環出來的都爲可迭代
也就是能夠將一個列表擴展到另外一個列表
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'seven', 'eric', 'seven',] 4 b = ["1", "2", "3"] 5 a.extend(b) 6 print(a) 7 #打印出 ['alex', 'seven', 'eric', 'seven', '1', '2', '3'] 將一個列表擴展到另外一個列表
index(self, value, start=None, stop=None)
"""(獲取一個元素在列表裏的索引下標)"""要獲取的元素
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 print(a.index("seven")) 5 #打印出 2 獲取到seven的索引下標爲2,默認從0開始因此是2
insert(self, index, p_object)
"""(插入元素)"""要插入的位置,要插入的元素
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 a.insert(0,"guixiu") 5 print(a) 6 #打印出 ['guixiu', 'alex', 'eric', 'seven'] 在0的位置插入guixiu
pop(self, index=None)
"""(移除元素)"""要移除的位置:默認移除後面一個元素
移除的元素還能夠從新賦值給一個變量
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 b = a.pop(0)#移除a變量的0位置元素,將移除的元素賦值給b 5 print(a) 6 #打印a ['eric', 'seven'] 移除了0位置的元素 7 8 print(b) 9 #打印b alex 被移除的元素
remove(self, value)
"""(移除某個元素)"""要移除的元素
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 a.remove("eric") 5 print(a) 6 #打印出 ['alex', 'seven'] 移除了eric
reverse(self)
"""(反轉元素順序)"""
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 a.reverse() 5 print(a) 6 #打印出 ['seven', 'eric', 'alex'] 反轉元素順序
刪除元素
索引方式刪除(刪除單個元素)
格式:del 列表變量[要刪除的下標]
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 del a[0] 5 print(a) 6 #打印出 ['eric', 'seven'] 刪除了0位置的元素
切片方式刪除(刪除多個元素)
格式:del 列表變量[開始位置:結束位置]
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ['alex', 'eric', 'seven'] 4 del a[0:2] 5 print(a) 6 #打印出 ['seven'] 刪除了0和2位置的元素
clear(self)
"""(移除列表全部元素)"""
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #list將16進制轉換成字符串 4 a = ['林', '貴', '秀'] 5 a.clear() 6 print(a)
enumerate(iterable,start=0 )
"""(自定義列表的下標開始位置)列表變量,要定義的開下標數:默認是0開始的"""
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ["電腦", "鼠標", "鍵盤", "顯示器"] 4 for k,v in enumerate(a,1):#由於是有鍵和值,因此循環要定義兩個變量k和v 5 print(k,v) 6 #打印出 7 # 1 電腦 8 # 2 鼠標 9 # 3 鍵盤 10 # 4 顯示器
enumerate()結合應用,輸入商品序號,打印出對應的商品
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #輸入商品序號,打印出對應的商品 4 a = ["電腦", "鼠標", "鍵盤", "顯示器"] 5 for k,v in enumerate(a,1):#默認下標號從0開始的enumerate(a,1)設置了從1開始 6 print(k,v) 7 #上面打印出商品的序號和名稱 8 b = input("請輸入商品序號")#等待用戶輸入商品序號 9 c = int(b)#將用戶輸入的序號轉換成數字類型 10 d = a[c-1]#將用戶輸入序號轉換成列表索引的下標,默認從0開始的因此要減一 11 print(d)#經過用戶輸入的索引下標打印出商品 12 13 # 1 電腦 14 # 2 鼠標 15 # 3 鍵盤 16 # 4 顯示器 17 # 請輸入商品序號4 18 # 顯示器
列表的嵌套,列表裏有字典,元組,
用索引的方法輸出須要的省份,省會,以及一個市
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #列表的嵌套,列表裏有字典,元組, 4 #用索引的方法輸出須要的省份,省會,以及一個市 5 a = ['四川', {'省會': '成都'}, ('自貢', '內江', '樂山'), '雲南', {'省會': '昆明'}, ('曲靖', '玉溪', '昭通')] 6 print(a[0])#=四川, 索引列表第0個元素 7 print(a[1])#{'省會': '成都'}, 索引列表第1個元素 8 print(a[1]["省會"])#=成都, 索引列表第1個元素,裏的字典的"省會"這個鍵 9 print(a[2])#=('自貢', '內江', '樂山') 索引列表裏的第二個元素 10 print(a[2][0])#=自貢 索引列表裏第2個元素,裏的元組的第0個元素 11 #最終輸出 12 # 四川 13 # {'省會': '成都'} 14 # 成都 15 # ('自貢', '內江', '樂山') 16 # 自貢
class list(object): """ list() -> new empty list list(iterable) -> new list initialized from iterable's items """ def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -- append object to end """ pass def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -- extend list by appending elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass def pop(self, index=None): # real signature unknown; restored from __doc__ """ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 """ pass def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __delslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__delslice__(i, j) <==> del x[i:j] Use of negative indices is not supported. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __iadd__(self, y): # real signature unknown; restored from __doc__ """ x.__iadd__(y) <==> x+=y """ pass def __imul__(self, y): # real signature unknown; restored from __doc__ """ x.__imul__(y) <==> x*=y """ pass def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __reversed__(self): # real signature unknown; restored from __doc__ """ L.__reversed__() -- return a reverse iterator over the list """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ """ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ L.__sizeof__() -- size of L in memory, in bytes """ pass __hash__ = None
列表的功能
轉換列表索引切片for循環長度刪除元素反轉排序追加插入移除元素索引位置統計元素個數擴展清除