python數據類型-列表

總結:Python3.6版本

  • 列表初始化:空列表、指定長度的列表、指定數據的列表【函數、列表解析】
  • 創建列表:list()【空列表】
  • 創建列表:list(iterable)【新列表-由可迭代對象的成員初始化】
  • 創建列表:[x for x in iterable]【列表解析創建列表】
  • 列表分類:單層、嵌套
  • 多維矩陣:嵌套模式組成的N行[外層成員數]*N列[內層成員數]
  • 列表長度:len(L)
  • 列表索引:L[i]【i從0開始】
  • 成員測試:x in L【返回布爾值】、for x in L:print(x)【遍歷】
  • 列表比較:【>、=、<=、==、!=】
  • 列表拼接:L1 + L2
  • 列表重複:L1 + L1 、L1 * N(N>=1)
  • 列表切片:L[a:b] 【正向從0開始,反向從-1開始,a、b均可以忽略,不包含後標】
  • 列表切片:L[a:b:c] 【a、b為區間,c為切片間隔,a、b均可以忽略,不包含後標】
  • 列表追加:L.append(obj)【返回None,追加對象-obj到原列表尾部】
  • 列表擴大:L.extend(iterable)【返回None,將可迭代對象成員追加到列表L尾部】
  • 列表插入:L.insert(index,obj)【返回None,在列表指定位置前插入數據】
  • 列表刪除:L.pop(index=None)【默認刪除最後一個成員可指定位置,函數返回刪除的成員】
  • 列表刪除:L.remove(value)【返回None,刪除給定的首先出現的值】
  • 列表查找:L.index(value, start=None, stop=None)【返回查詢值首次出現的索引位置】
  • 列表計數:L.count(value)【返回給定數據出現的次數】
  • 列表排序:L.sort(key=None, reverse=False)【默認為升序,key為一個可調用函數】
  • 列表反轉:L.reverse()【返回None,反轉列表中的數據】
  • 列表拷貝:L.copy()【返回一個淺拷貝的列表對象,單層拷貝與深拷貝的區別後邊解釋】
  • 列表清空:L.clear()【返回None,清空列表中的數據使原列表為空列表】
python數據類型-列表


<code># 空列表【手動創建、類實例化】
# 【運算符重載方法__init__解釋-->list()調用】
print([]) # []
print(list()) # []

# 指定長度的列表
print([0]*5) # [0, 0, 0, 0, 0]
print([0 for x in range(5)]) # [0, 0, 0, 0, 0]
My_list = []
for i in range(5):
My_list.append(0)
print(My_list) # [0, 0, 0, 0, 0]

# 指定數據的列表【list創建、列表解析創建】
print(list(range(5))) # [0, 1, 2, 3, 4]
print(list("test")) # ['t', 'e', 's', 't']
print([x for x in "test"]) # ['t', 'e', 's', 't']

# 無嵌套的列表
print([1, '2', 3]) # [1, '2', 3]

# 嵌套的列表
print(["123", 456, ["12", 34]]) # ['123', 456, ['12', 34]]

# 列表長度【返回長度,運算符重載方法,長度運算,相當於調用了__len__方法】
print(len([1, 2, 3])) # 3
print([1, 2, 3].__len__()) # 3

# 列表索引【正向0標開始,反向-1標開始】
# 【返回索引值,運算符重載方法,索引運算,相當於調用了__getitem__方法】
for x in range(3):
print([1, 2, 3][x], end=" ") # 1 2 3
print("", [1, 2, 3][-1]) # 3

print([1, 2, 3].__getitem__(-1)) # 3

# 成員測試【返回布爾值,運算符重載方法,成員關係測試in,相當於調用了__contains__方法】
print(3 in [1, 2, 3]) # True
for x in [1, 2, 3]:
print(x, end=" ") # 1 2 3
print([1, 2, 3].__contains__(3)) # True

# 列表比較【返回布爾值,運算符重載方法,特定的比較,調用方式(,<=,>=,==,!=)】
print([1, 2, 3] == [1, 2, 3]) # True
print([1, 2, 3].__eq__([1, 2, 3])) # True

print([1, 2, 3] != [1, 2, 3]) # False
print([1, 2, 3].__ne__([1, 2, 3])) # False

print([1, 2, 3] > [1, 2, 3]) # False
print([1, 2, 3].__gt__([1, 2, 3])) # False

print([1, 2, 3] < [1, 2, 3]) # False
print([1, 2, 3].__lt__([1, 2, 3])) # False

print([1, 2, 3] >= [1, 2, 3]) # True
print([1, 2, 3].__ge__([1, 2, 3])) # True

print([1, 2, 3] <= [1, 2, 3]) # True
print([1, 2, 3].__le__([1, 2, 3])) # True

# 列表拼接【運算符重載方法 運算符a+b、a+=b】
# 【__add__-->a+b,生成的是一個新對象
# 【__iadd__-->a+=b,是拼接在對象a身上,不等於a=a+b,這a引用了新對象】
a = [1, 2]
b = [3]
c = a + b
print(c, a) # [1, 2, 3] [1, 2]
print(a.__add__(b), a) # [1, 2, 3] [1, 2]

a = [1, 2]
b = [3]
print(id(a)) # 54128728
a += b

print(a, id(a)) # [1, 2, 3] 54128728
print(a.__iadd__(b), a, id(a)) # [1, 2, 3, 3] [1, 2, 3, 3] 54128728

# 列表重複【運算符重載方法 運算符a*b、a*=b】
# 【__mul__-->a*b,生成的是一個新對象、
# 【__imul__-->a*=b,是重複在對象a身上,不等於a=a*b,這a引用了新對象】
a = [1]
b = 3
print(a.__mul__(b), a) # [1, 1, 1] [1]

a = [1]
b = 3
print(id(a)) # 8450136
a *= b
print(a, id(a)) # [1, 1, 1] 8450136

a = [1]
b = 3
print(id(a)) # 11620184
print(a.__imul__(b), a, id(a)) # [1, 1, 1] [1, 1, 1] 11620184


# 列表切片【正向從0標開始,反向走-1標開始】
# L[a:b]正向操作【等價於L[a:b:1]】,a、b可以為負,表示的是字符正反向的座標
# L[a:b:c]【c為整數正向操作--ab】
print([1, 2, 3][:]) # [1, 2, 3]
print([1, 2, 3][::-1]) # [3, 2, 1]
print([1, 2, 3][0:1]) # [1]
print([1, 2, 3][-2:-1]) # [2]
print([1, 2, 3, 4][0:3:2]) # [1, 3]
print([1, 2, 3, 4][3:0:-2]) # [4, 2]
print([1, 2, 3, 4][-1:-4:-2]) # [4, 2]

# 列表追加【append函數返回None,追加再原列表尾部】
My_list = [1, 2, 3]
print(My_list.append(4)) # None
print(My_list) # [1, 2, 3, 4]

# 列表擴大【extend函數返回None,將可迭代對象的成員追加到列表尾部擴大列表】
My_list = [1]
print(My_list.extend('2')) # None
print(My_list.extend([3])) # None
print(My_list.extend((3,))) # None
print(My_list) # [1, 2, 3, 4]

# 列表插入【insert函數返回None,在列表指定的位置前插入數據】
My_list = [2, 3, 4]
print(My_list.insert(0, 1)) # None
print(My_list) # [1, 2, 3, 4]

# 列表刪除【pop函數返回刪除的值,也可指定索引位置刪除】
My_list = [1, 2, 3, 4]
print(My_list.pop()) # 4
print(My_list) # [1, 2, 3]
print(My_list.pop(2)) # 3
print(My_list) # [1, 2]

# 列表刪除【remove函數返回None,刪除指定的值】
My_list = [1, 2, 3, 4, 3, 5]
print(My_list.remove(3)) # None
print(My_list) # [1, 2, 4, 3, 5]

# 列表查詢【index函數返回查詢值首次出現的位置】
My_list = [1, 2, 3, 4, 3]
print(My_list.index(3)) # 2
print(My_list.index(3, 0, len(My_list))) # 2

# 列表計數【count函數返回給定數據出現的次數】
My_list = [1, 2, 3, 4, 3]
print(My_list.count(3)) # 2

# 列表排序【sort函數返回None,默認升序reverse=False】
# 【key為一個可調用函數,針對成員適用函數】

My_list = [2, 4, 1, 3]
print(My_list.sort(key=None, reverse=False)) # None
print(My_list) # [1, 2, 3, 4]

My_list = ['aaa', 'c', 'bbb']
print(My_list.sort(key=len, reverse=False)) # None
print(My_list) # ['c', 'aaa', 'bbb']

My_list = ['aaa', 'c', 'bbb']
print(My_list.sort()) # None
print(My_list) # ['aaa', 'bbb', 'c']

My_list =[1.2, 1.4, 1.33]
print(My_list.sort(key=int, reverse=False)) # None
print(My_list) # [1.2, 1.4, 1.33]

# 列表反轉
My_list = [1, 2, [3, 4]]
print(My_list.reverse()) # None
print(My_list) # [[3, 4], 2, 1]

# 列表拷貝【copy函數返回淺拷貝的列表】
# 【淺拷貝只拷貝一層,修改拷貝數據可能會修改元數據,深拷貝則不會,後邊解釋】
My_list = [1, 2, [3, 4]]
My_copy = My_list.copy()
print(My_copy) # [1, 2, [3, 4]]
print(id(My_list), id(My_copy)) # 58129184 57435560
for x in range(len(My_list)):
print(id(My_list[x]), id(My_copy[x])) # [493347600 493347600 493347616 493347616 58126424 58126424]

# 列表清空【clear函數返回None,清空列表中的數據】
My_list = [1, 2, 3, 4]
print(My_list.clear()) # None
print(My_list) # []
/<code>

原型類或者方法:

<code>class list(object):
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

#空參數返回空列表、參數為可迭代對象則返回由可迭代對象成員初始化的新列表

def append(self, p_object):
L.append(object) -> None -- append object to end
pass
#列表尾部追加對象,返回None

def clear(self):
L.clear() -> None -- remove all items from L
pass
#清空列表中的數據使原列表為空列表,返回None

def copy(self):
L.copy() -> list -- a shallow copy of L return []
#返回一個淺拷貝的列表對象

def count(self, value):
L.count(value) -> integer -- return number of occurrences of value
return 0
#返回給定數據出現的次數

def extend(self, iterable):
L.extend(iterable) -> None -- extend list by appending elements from the iterable
pass
#將可迭代對象成員追加到列表尾部,返回None

def index(self, value, start=None, stop=None):
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
return 0
#返回所查詢值的首先出現的索引位置,值不存在拋出ValueError

def insert(self, index, p_object):
L.insert(index, object) -- insert object before index
pass
#在列表指定位置前插入數據,返回None


def pop(self, index=None):
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
#默認刪除列表尾部的值,也可指定位置,返回刪除的值。
#如果列表為空或者是超出索引則拋出IndexError

def remove(self, value):
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
pass
#刪除給定的值首先出現的位置,返回None,值不存拋出ValueError

def reverse(self):
L.reverse() -- reverse *IN PLACE*
pass
#反轉列表中的數據,返回None

def sort(self, key=None, reverse=False):
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
pass
#對列表進行排序,默認為升序

def __add__(self, *args, **kwargs):
Return self+value.
pass
#運算符重載方法【運算符+,調用:L + L1】

def __contains__(self, *args, **kwargs):
Return key in self.
pass
#運算符重載方法【成員關係測試,調用:var in L】

def __delitem__(self, *args, **kwargs):
Delete self[key].
pass
#刪除數據 del a[x]


def __eq__(self, *args, **kwargs):
Return self==value.
pass
#運算符a==b

def __getattribute__(self, *args, **kwargs):
Return getattr(self, name).
pass
#屬性獲取

def __getitem__(self, y):
x.__getitem__(y) <==> x[y]
pass
#索引運算【a=[1,2,3],a[1]等於a.__getitem__[1]】

def __ge__(self, *args, **kwargs):
Return self>=value.
pass
#運算符a>=b

def __gt__(self, *args, **kwargs):
Return self>value.
pass
#運算符a>b

def __iadd__(self, *args, **kwargs):
Implement self+=value.
pass
#運算符a+=b 作用於a

def __imul__(self, *args, **kwargs):
Implement self*=value.
pass
運算符a*=b 作用於a

def __init__(self, seq=()):
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
# (copied from class doc)
pass
#運算符重載方法【構造函數,調用:my_list = list(seq=())】

def __iter__(self, *args, **kwargs):
Implement iter(self).
pass

#返回一個迭代器

def __len__(self, *args, **kwargs):
Return len(self).
pass
#返回len函數,可顯示調用__len__

def __le__(self, *args, **kwargs):
Return self<=value.
pass
#運算符a<=b

def __lt__(self, *args, **kwargs):
Return self<value.> pass
#運算符a
def __mul__(self, *args, **kwargs):
Return self*value.n
pass
#運算符a*b

@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
Create and return a new object. See help(type) for accurate signature.
pass
#創造並返回一個新對象

def __ne__(self, *args, **kwargs):
Return self!=value.
pass
#運算符!=

def __repr__(self, *args, **kwargs):
Return repr(self).
pass
#更好的顯示數據

def __reversed__(self):
L.__reversed__() -- return a reverse iterator over the list
pass
#返回一個反轉迭代器

def __rmul__(self, *args, **kwargs):
Return self*value.
pass

#列表重複運算a*=b 作用結果在a上


def __setitem__(self, *args, **kwargs):
Set self[key] to value.
pass
#索引賦值運算【a=[1,2,3,4],a[0]=2等於a.__setitem__(0,2)】

def __sizeof__(self):
L.__sizeof__() -- size of L in memory, in bytes
pass
#列表在內存中佔用的字節數

__hash__ = None
/<value.>/<code>


分享到:


相關文章: