zoukankan      html  css  js  c++  java
  • 自学Python2.2-基本数据类型-列表list(object)

    Python List方法总结 

    一、 列表简介:

       列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等
       列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作
       可以通过list(seq)函数把一个序列类型转换成一个列表

    运算符: 索引运算[i] ,切片运算[i:j], 扩展切片运算[i:j:stride]

    支持运算:索引,切片,min(),max(),len()等

    1. 列表赋值:

    l1=[] 空列表   #但是内存有位置存放,使用id(l1)查看,输出45623200 

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

    l3=[1,'b']

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

    2.列表操作

    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]

    二、列表的方法

    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]  

  • 相关阅读:
    超链接
    Image中的alt
    预格式
    json字符串转json对象,json对象转换成java对象
    google-gson 解析json
    java HTTP请求工具
    JS文件中获取contextPath的方法
    eclipse中安装freemarker插件及ftl使用freemarker编辑器
    MyBatis 传入参数之parameterType
    typeAliases别名
  • 原文地址:https://www.cnblogs.com/yaoyaojcy/p/7409761.html
Copyright © 2011-2022 走看看