python中list的常用函数

List的常用函数

简单的函数有:

  • cmp(list1,list2) 比较两个列表的元素 (python3.0后删去,直接用>,==,<即可)
  • len(list) 列表元素个数
  • max(list) 返回列表元素最大值
  • min(list) 返回列表元素最小值
  • list(seq) 将可迭代容器转换为列表 (如果是字典的话,就只保留键)

接下的函数最好单独一行,不要出现print(list.append())这样的语句,会打印出None,而不是所期望的值。

  1. list1.append(obj)

    • 末尾增加元素
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc']
    aList.append( 2009 )
    print ("Updated List : ", aList)
    # result
    Updated List :  [123, 'xyz', 'zara', 'abc', 2009]
  2. list2.count(obj)

    • 统计obj的个数
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc', 123]
    
    print ("Count for 123 : ", aList.count(123))
    print ("Count for zara : ", aList.count('zara'))
    # result
    Count for 123 :  2
    Count for zara :  1
  3. list3.count(obj)

    • extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
    python 复制代码
    list1 = [1,2,3,4]
    dict1 = {'a':1,'b':2}
    
    list1.extend(dict1)
    print(list1)
    # result
    [1, 2, 3, 4, 'a', 'b']
    
    aList = [123, 'xyz', 'zara', 'abc', 123]
    bList = [2009, 'manni']
    aList.extend(bList)
    
    print ("Extended List : ", aList)
    # result
    Extended List :  [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
  4. list4.index(x, start\[, end])

    • x-- 查找的对象。
    • start-- 可选,查找的起始位置。
    • end-- 可选,查找的结束位置。
    python 复制代码
    aList = ['runoob', 'xyz', 'runoob', 'abc']
    
    print ("runoob 索引位置: ", aList.index( 'runoob' ))
    print ("runoob 索引位置 : ", aList.index( 'runoob', 1, 3 ))
    # result
    runoob 索引位置:  0
    runoob 索引位置 :  2
  5. list5.insert()

    • insert(x,obj) 函数用于将指定对象插入列表的指定位置。(这里的插入是指第x对象的前面)
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc']
    blist = [1,2,3,5,6]
    aList.insert(3, blist)
    
    print("Final List : ", aList)
    # result
    Final List :  [123, 'xyz', 'zara', [1, 2, 3, 5, 6], 'abc']
  6. list6.pop()

    • pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
    • list.pop(index=-1)
    • obj -- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
    python 复制代码
    list1 = ['Google', 'Runoob', 'Taobao']
    list_pop = list1.pop(1)
    print("删除的项为 :", list_pop)
    print("列表现在为 : ", list1)
    # result
    删除的项为 : Runoob
    列表现在为 :  ['Google', 'Taobao']
  7. list7.remove()

    • remove() 函数用于移除列表中某个值的第一个匹配项。
    • list.remove(obj)
    • obj -- 列表中要移除的对象
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc', 'xyz']
    
    aList.remove('xyz')
    print ("List : ", aList)
    aList.remove('abc')
    print ("List : ", aList)
    # result
    List :  [123, 'zara', 'abc', 'xyz']
    List :  [123, 'zara', 'xyz']
  8. list8.reverse()

    • reverse() 函数用于反向列表中元素。
    • list.reverse()
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc', 'xyz']
    
    aList.reverse()
    print ("List : ", aList)
    # result
    List :  ['xyz', 'abc', 'zara', 'xyz', 123]
  9. list9.sort()

    • sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
    • list.sort(cmp=None, key=None, reverse=False)
    • cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
    • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
    • reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
    • 值得注意的是排序语句只能单独一条 不能和其他的语句如print一起使用,如print(list1.sort())不会报错但是打印出来是None。
    python 复制代码
    def takeSecond(elem):
     return elem[1]
    
    # 列表
    random = [(2, 2), (3, 4), (4, 1), (1, 3)]
    
    # 指定第二个元素排序
    random.sort(key=takeSecond)
    
    # 输出类别
    print('排序列表:')
    print(random)
    # result
    排序列表:
    [(4, 1), (2, 2), (1, 3), (3, 4)]
  10. list10.extendI(seq)

    • list.extend(seq)
    • seq -- 元素列表。
    python 复制代码
    aList = [123, 'xyz', 'zara', 'abc', 123]
    bList = [2009, 'manni']
    aList.extend(bList)
    
    print ("Extended List : ", aList )
    # result
    Extended List :  [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
相关推荐
岁月宁静5 小时前
五、《从零手撸 Agent》 — 什么是 Agent:从“会聊天”到“会做事”,差的就是一个循环
python·agent
梯度下降者6 小时前
Python - 函数
python·高阶函数·可调用对象·functools·嵌套作用域
starzy19908 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
wuyk5559 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
Vex2une12 小时前
JDK 8 下载安装与环境变量配置完整指南(Windows新手图文教程)
java·windows·环境配置·环境变量·jdk8·jdk下载安装·java入门教程
kyle~15 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享15 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者16 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.17 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人17 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python