python基础之列表

  • 列表(list)


列表(list)

定义:列表是一种有序的集合,列表定义后仍可变,可以随时添加和删除列表中的元素,创建方式为:列表名称 = [元素1, 元素2, ……]
特点:list中的元素可以是字符串,数字,列表,元组,字典等

注意:元素中需要使用英文逗号隔开,如果是字符串,需要使用英文引号

示例:

list_1 = ['test',1,2,3] #创建一个list
   	dict_1 = dict(a=1,b=2,c=3) #创建一个dict
   	tuple_1 = (1,2,3) #创建一个tuple
    list_2 = [list_1,dict_1,tuple_1] #创建一个元素包括list,dict和tuple的list
    print(list_1)
    print(list_2)

内置函数:

list_3 = [1,2,3,6,5,4]
    list_3.append(1) #list最后追加一个元素,可以是数字,字符串,也可以是list,tuple
    list_3.clear() #清空该list
    list_3c = list_3.copy() #复制一个相同的list并返回,需要用一个变量接收
    num = list_3.count(1) #返回list中该元素的出现次数,需要使用一个变量接收
    list_3.extend(list_3) #通过一个可迭代的元素扩展list,注意,必须是可迭代的元素,不能是常量
    num_index = list_3.index(1) #返回某个元素在该list中第一次出现的位置,需要变量接收,如该值不存在则报错not in list
    list_3.insert(0,8) #在某一个索引位插入一个元素,索引位超过list长度时默认在最后一位追加
    num_pop = list_3.pop(0) #删除指定索引位置的元素并返回该元素,索引值超过最大长度则报错pop index out of range
    list_3.remove(1) #删除第一次出现的指定元素,如无此元素则报错list.remove(x): x not in list
    list_3.reverse() #list字符串反转
    list_3.sort() #对list进行升序排序,可加反转函数进行倒序排序,如list_3.sort(reverse=True)
    #根据需要自己注释对应函数并打印结果验证
    print(list_3)
    print(list_3c)
    print(num)
    print(num_index)
    print(num_pop)

常用内置函数部分源码:

class list(object):
    """
    Built-in mutable sequence.
    
    If no argument is given, the constructor creates a new empty list.
    The argument must be an iterable if specified.
    """
    def append(self, *args, **kwargs): # real signature unknown
        """ Append object to the end of the list. """
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all items from list. """
        pass

    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of the list. """
        pass

    def count(self, *args, **kwargs): # real signature unknown
        """ Return number of occurrences of value. """
        pass

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend list by appending elements from the iterable. """
        pass

    def index(self, *args, **kwargs): # real signature unknown
        """
        Return first index of value.
        
        Raises ValueError if the value is not present.
        """
        pass

    def insert(self, *args, **kwargs): # real signature unknown
        """ Insert object before index. """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return item at index (default last).
        
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove first occurrence of value.
        
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self, *args, **kwargs): # real signature unknown
        """ Reverse *IN PLACE*. """
        pass

    def sort(self, *args, **kwargs): # real signature unknown
        """
        Sort the list in ascending order and return None.
        
        The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
        order of two equal elements is maintained).
        
        If a key function is given, apply it once to each list item and sort them,
        ascending or descending, according to their function values.
        
        The reverse flag can be set to sort in descending order.
        """
        pass

个人笔记,手打不易,如有错误,望君指正