zoukankan      html  css  js  c++  java
  • Python 学习笔记(八)Python列表(二)

    列表函数

    追加和扩展

    list.append() 在列表末尾追加新的对象

     1 >>> dir(list)  #dir 查看列表的函数
     2 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
     3 >>> help(list.append) #help 查看 list.append() 函数的详细内容
     4 Help on method_descriptor:
     5 
     6 append(...)
     7     L.append(object) -- append object to end  #将对象追加到末尾
     8 
     9 >>> a =[2,6]
    10 >>> a.append(50)  #将对象50追加到列表a中
    11 >>> a
    12 [2, 6, 50]
    13 >>> a.append("python book") 
    14 >>> a
    15 [2, 6, 50, 'python book']
    16 >>> a
    17 [2, 6, 50, 'python book', ['baidu', 'weibo']]
    18 >>> b =[1]
    19 >>> id(b)   #id 返回值给出在内存中的空间
    20 60126664L
    21 >>> b.append(5)
    22 >>> b
    23 [1, 5]
    24 >>> b
    25 [1, 5]
    26 >>> id(b) #追加5 之后,id返回值 一样
    27 60126664L
    28 >>> b.append("zhangsan")
    29 >>> id(b)
    30 60126664L
    31 >>> b
    32 [1, 5, 'zhangsan']
    33 >>>
    注:列表在被修改的时候,不是创建了一个新的列表,而是修改了原来的列表,这种修改称为原地修改

    extend()在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

     1 >>> help(list.extend) #help查看list.extend函数的详细内容
     2 Help on method_descriptor:
     3 
     4 extend(...)
     5     L.extend(iterable) -- extend list by appending elements from the iterable  #把可迭代对象中的元素追加到列表中
     6 
     7 >>> a=[1,2,3]  #可迭代对象
     8 >>> b=[4,5,6]  #可迭代对象
     9 >>> a.extend(b) 
    10 >>> a           #b列表中的元素被一个个追加到a列表中
    11 [1, 2, 3, 4, 5, 6] 
    12 >>> a.extend("python")
    13 >>> a
    14 [1, 2, 3, 4, 5, 6, 'p', 'y', 't', 'h', 'o', 'n']
    15 >>> alst =[1,2]20 >>> hasattr(alst,'__iter__') #判断是否可迭代,是返回true ,否则false
    21 True
    22 >>> hasattr("python",'__iter__') #字符串不可迭代,此处是将字符串一个的字符拆出追加
    23 False
    24 >>> a =[1,2]
    25 >>> a.append([4,5])
    26 >>> a
    27 [1, 2, [4, 5]]
    28 >>> a.extend([4,5]) 
    29 >>> a
    30 [1, 2, [4, 5], 4, 5]
    31 >>> b =[9,8]
    32 >>> a.append(b[0])
    33 >>> a.append(b[1])
    34 >>> a
    35 [1, 2, [4, 5], 4, 5, 9, 8]
    36 >>>
    注:append 与extend 区别就是,extend 将一个个的元素拆分追加,append是整体追加

    其他函数

    count() 统计某个元素在列表中出现的次数

     1 >>> help(list.count)
     2 Help on method_descriptor:
     3 
     4 count(...)
     5     L.count(value) -> integer -- return number of occurrences of value  #返回出现的次数
     6 
     7 >>> a =[1,1,1,2,2,2,3,3,3]
     8 >>> a.count(1)   #1这个元素在列表中出现3次
     9 3
    10 >>> a.count(2)  #2这个元素在列表中出现3次
    11 3
    12 >>> a.count("a") #a在列表中没有
    13 0

    index() 从列表中找出某个值第一个匹配项的索引位置

     1 >>> help(list.index)
     2 Help on method_descriptor:
     3 
     4 index(...)
     5     L.index(value, [start, [stop]]) -> integer -- return first index of value. #参数中的value值在列表中第一次出现的索引位置
     6     Raises ValueError if the value is not present.
     7 
     8 >>> a.index(1)
     9 0
    10 >>> a.index(2)
    11 3
    12 >>>

    insert() 将对象插入列表

     1 >>> help(list.insert)
     2 Help on method_descriptor:
     3 
     4 insert(...)
     5     L.insert(index, object) -- insert object before index  #把对象插入到索引所对应的元素的前面
     6 
     7 >>> a =["python","web"]
     8 >>> a.insert(1,"aaa")   #在索引是1的元素的前面插入一个字符串aa
     9 >>> a
    10 ['python', 'aaa', 'web']
    11 >>> a.insert(0,"like") #在最前面插入一个字符串like
    12 >>> a
    13 ['like', 'python', 'aaa', 'web']
    14 >>>
    注:insert为原地修改,没有新建一个列表

    pop() 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

     1 >>> help(list.pop)
     2 Help on method_descriptor:
     3 
     4 pop(...)
     5     L.pop([index]) -> item -- remove and return item at index (default last).  #删除索引所对应的元素,并它作为返回值返回 (默认删除最后一个元素)
     6     Raises IndexError if list is empty or index is out of range. #不能删除为空的或者超出索引范围的元素,否则索引错误
     7 
     8 >>> a
     9 ['like', 'python', 'aaa', 'web']
    10 >>> a.pop(1)
    11 'python'
    12 >>> a
    13 ['like', 'aaa', 'web']
    14 >>>

    remove() 移除列表中某个值的第一个匹配项

     1 >>> help(list.remove)
     2 Help on method_descriptor:
     3 
     4 remove(...)
     5     L.remove(value) -- remove first occurrence of value.  #移除某个值得第一个匹配项
     6     Raises ValueError if the value is not present.
     7 
     8 >>> a =["test","test","demo"]
     9 >>> a.remove("test")
    10 >>> a
    11 ['test', 'demo']
    12 >>> a.remove("aa")
    13 Traceback (most recent call last):
    14   File "<stdin>", line 1, in <module>
    15 ValueError: list.remove(x): x not in list
    16 >>>

    reverse()  反向列表中元素

     1 >>> help(list.reverse)
     2 Help on method_descriptor:
     3 
     4 reverse(...)
     5     L.reverse() -- reverse *IN PLACE*
     6 
     7 >>> a =[1,2,3,4,5]
     8 >>> a.reverse()
     9 >>> a
    10 [5, 4, 3, 2, 1]
    11 >>>

    sort() 对原列表进行排序

     1 >>> help(list.sort)
     2 Help on method_descriptor:
     3 
     4 sort(...)
     5     L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
     6     cmp(x, y) -> -1, 0, 1
     7 
     8 >>> a =[5,9,3,1]
     9 >>> a.sort()  #从小到大排列
    10 >>> a
    11 [1, 3, 5, 9]
    12 >>> b=[9,3,8,6]
    13 >>> b.sort(reverse=True) #从大到小排列
    14 >>> b
    15 [9, 8, 6, 3]
    16 >>>
  • 相关阅读:
    347. Top K Frequent Elements
    437. Path Sum III
    338. Counting Bits
    337. House Robber III
    494. Target Sum
    416. Partition Equal Subset Sum
    LINUX 使用grep命令查看某个指定时间段的日志
    git 常用命令操作
    Python之IDE工具下载安装及注册详解及创建项目
    Python下载安装及验证
  • 原文地址:https://www.cnblogs.com/wangruihua-521/p/8549434.html
Copyright © 2011-2022 走看看