自學Python2.2-基本數據類型-列表list(object)

Python List方法總結 

1、 列表簡介:python

   列表是序列對象,可包含任意的Python數據信息,如字符串、數字、列表、元組等
   列表的數據是可變的,咱們可經過對象方法對列表中的數據進行增長、修改、刪除等操做
   能夠經過list(seq)函數把一個序列類型轉換成一個列表
app

運算符: 索引運算[i] ,切片運算[i:j], 擴展切片運算[i:j:stride]ide

支持運算:索引,切片,min(),max(),len()等函數

1. 列表賦值:post

l1=[] 空列表   #可是內存有位置存放,使用id(l1)查看,輸出45623200 spa

l2=[1,2,3,4,5,6,] rest

l3=[1,'b']code

l4=[[1,2],['a','b']]  嵌套列表對象

2.列表操做blog

l2=[1,2,3,4,5,6,]
l2[3]=78
print(l2) 

 輸出[1, 2, 3, 78, 5, 6]

l2=[1,2,3,4,5,6,]
l2[4]='xyz'
print(l2)

輸出[1, 2, 3, 4, 'xyz', 6]

l2=[1,2,3,4,5,6,]
result=l2[1:3] #表示分片偏移,第一個數值表示提取的第一個元素編號,包含在分片內;第二個數值表示分片後剩餘的的第一個元素編號,不包含在分片內
print(result)

輸出[2, 3]

l2=[1,2,3,4,5,6,]
l2[1:3]=[] #表示刪除, 也能夠使用del([l2[1:3]])
print(l2)

輸出[1, 4, 5, 6]

2、列表的方法

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) -> None -- append object to end """
        pass

    def clear(self): # real signature unknown; restored from __doc__
        """ L.clear() -> None -- remove all items from L """
        pass

    def copy(self): # real signature unknown; restored from __doc__
        """ L.copy() -> list -- a shallow copy of L """
        return []

    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) -> None -- 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) -> None -- 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, key=None, reverse=False): # real signature unknown; restored from __doc__
        """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
        pass

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __delitem__(self, *args, **kwargs): # real signature unknown
        """ Delete self[key]. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iadd__(self, *args, **kwargs): # real signature unknown
        """ Implement self+=value. """
        pass

    def __imul__(self, *args, **kwargs): # real signature unknown
        """ Implement self*=value. """
        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, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @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): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ L.__reversed__() -- return a reverse iterator over the list """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

    def __setitem__(self, *args, **kwargs): # real signature unknown
        """ Set self[key] to value. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ L.__sizeof__() -- size of L in memory, in bytes """
        pass

    __hash__ = None
list源碼

1.append(self, p_object),在列表尾部追加單個對象x,使用多個參數會引發異常

list=[1,2,3,4,5,]
list.append(88)
print(list)

輸出[1, 2, 3, 4, 5, 88]  

l1=[1,2,3,4,5,]
l2=['carlos',77,]
l1.append(l2)
print(l1)

輸出[1, 2, 3, 4, 5, ['carlos', 77]]

2. clear(self) 

l1=[1,2,3,4,5,]
l1.clear()
print(l1)

輸出[] 

3. copy(self)

l1=[1,2,3,4,5,]
l2=l1.copy()
print(l1)
print(l2)

輸出

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

4. count(self, value),返回對象value在列表中出現的次數

l1=['to',2,'be','to',5,]
result=l1.count('to')
print(result)

輸出2

l1=[[1,2],'alex','be','to',[1,2],[2],[1,2],]
result=l1.count([1,2])
print(result)

輸出3

5.  extend(self, iterable),將列表中的表項添加到列表中,返回None

l1=[1,2,3,4,5,6,]
print(id(l1))
l2=['carlos',99,[3,7],]
l3=l1.extend(l2,)
print(l1)   #extend修改被擴展序列
print(id(l1))

輸出

33039344
[1, 2, 3, 4, 5, 6, 'carlos', 99, [3, 7]]
33039344

l1=[1,2,3,4,5,6,]
print(id(l1))
l2=['carlos',99,[3,7],]
l3=l1+l2
print(l3)   # 原始鏈接的操做是返回一個全新的列表
print(id(l3))

輸出  

38806512
[1, 2, 3, 4, 5, 6, 'carlos', 99, [3, 7]]
38807592

6. index(self, value, start=None, stop=None),返回列表中匹配對象value的第一個列表項的索引,無匹配元素時產生異常

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,]
l2=l1.index(5)
print(l2)   # 返回列表中匹配對象value的第一個列表項的索引

輸出4

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,]
l2=l1.index(5,5,10)
print(l2)   # 返回列表中匹配對象value的索引5,10 以前匹配的項

輸出9  

7.insert(self, index, p_object),在索引爲i的元素前插入對象x,如list.insert(0,x)在第一項前插入對象,返回None

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,]
l1.insert(3,'alex')
print(l1) 

輸出[1, 2, 3, 'alex', 4, 5, 6, 'carlos', 99, [3, 7], 5]

8.pop(self, index=None),刪除列表中索引爲x的表項,並返回該表項的值,若未指定索引,pop返回列表最後一項

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,]
l1.pop()  # 默認最後一個元素
print(l1)

輸出[1,2,3,4,5,6,'carlos',99,[3,7]]

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,]
l1.pop(5)  # 移除索引5的元素
print(l1)

輸出[1,2,3,4,5,'carlos',99,[3,7],5]

9.remove(self, value),刪除列表中匹配對象x的第一個元素,匹配元素時產生異常,返回None

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,88,5,102,]
l1.remove(5)  # 移除匹配項5的一個元素
print(l1)

輸出[1,2,3,4,6,'carlos',99,[3,7],5,88,5,102,]

10. reverse(self),顛倒列表元素的順序

l1=[1,2,3,4,5,6,'carlos',99,[3,7],5,88,5,102,]
l1.reverse()
print(l1)

輸出[102, 5, 88, 5, [3, 7], 99, 'carlos', 6, 5, 4, 3, 2, 1]

11 .sort(self, key=None, reverse=False),對列表排序,返回none,bisect模塊可用於排序列表項的添加和刪除

l1=[1,25,63,4,5,6,99,5,88,15,102,]
l1.sort()
print(l1)

輸出[1, 4, 5, 5, 6, 15, 25, 63, 88, 99, 102]

  當須要一個排好序的列表副本,同時又要保留原有列表不變的正確作法以下:

l1=[1,25,63,4,5,6,99,5,88,15,102,]
l2=l1[:]
l2.sort()
print(l1)
print(l2)

輸出

[1, 25, 63, 4, 5, 6, 99, 5, 88, 15, 102][1, 4, 5, 5, 6, 15, 25, 63, 88, 99, 102]  

相關文章
相關標籤/搜索