zoukankan      html  css  js  c++  java
  • python 列表和元组

    一,基本的列表操作

    1.该表列表,元素赋值

    示例:

    >>>x = [1,1,1]

    >>>x[1] = 2

    >>>x

    [1,2,1]

    2.删除元素

    使用del语句来实现

    >>>names = ['a' ,'b','c','d','f']

    >>>del names[2] #删除索引为2的元素

    >>>names

    ['a','b','d','f']

    3.分片赋值:

    >>>name = list('perl')

    >>>name

    ['p' ,'e' ,'r' ,'l']

    >>>name[2: ]=list('ar')

    >>>name

    ['p' ,'e' ,'a' ,'r']

    二、列表方法

    1.append()方法 #用户在列表末尾追加新的对象

    >>>lst = [1, 2 ,3]

    >>>lst.append(4)

    >>>lst

    [1, 2, 3 ,4]

    2.count()方法 #统计某个元素在列表中出现的次数

    >>>['to' ,'be' ,'or' ,'not' ,'to' ,'be'].count('to')

    2

    >>>x = [[1.2], 1 ,1 [2 ,1 ,[1 ,2]]]

    >>>x.count([1, 2])

    1

    3.extend() # 方法可以在列表的末尾一次性追加另一个序列中的多个值,

    >>>a = [1, 2, 3]

    >>>b = [4, 5, 6]

    >>>a.extend(b)

    >>>a

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

    4.index()

    #用于从列表中找出某个值得索引位置

    >>> knights = ['we', 'are' ,'the' ,'who']

    >>>knights.index('the')

    2

    5.insert()函数

    #方法用户将对象插入到列表中

    >>>number = [1,2,3,4,5,6]

    >>>number.insert(3,'four')

    >>>number

     [1,2,3,four,5,6]

    6.pop()方法

    #该方法会移除列表中的一个元素(默认是最后一个)

    >>>x = [1, 2, 3]

    >>>x.pop()

    3

    7.remove()

    #方法用于移除列表中某个值的第一个匹配项:

    >>>x = ['to' , 'be', 'or']

    >>>x.remove('be')

    >>>x

    ['to' , 'or']

    8.reverse()

    #方法将列表中的元素反向存放

    x = [1, 2, 3]

    x.reverse()

    x

    结果:[3, 2, 1]

    9.sort()

    #该方法用于在原始位置对列表进行排序

    x = [4, 6, 2, 1, 7, 9]

    x.sort()

    x

    结果:

    [1,2,4,6,7,9]

    本章的新函数

    函数 描述
    cmp(x,y) 比较两个值
    len(seq) 返回序列的长度
    list(seq) 把序列转换成列表
    max(args) 返回序列或者参数集合中的最大值
    min(args) 返回序列或者参数集合中的最小值
    reversed(seq) 对序列进行反向迭代    
    sorted(seq) 返回已排序的包含seq所有元素的列表
    tuple(seq) 把序列转换成元组
  • 相关阅读:
    lock,Monitor,Mutex的区别
    byte[]数组和int之间的转换
    接口测试总结
    python接口自动化测试(七)unittest 生成测试报告
    python 接口自动化测试(六)使用unittest 批量用例管理
    python 接口自动化测试(五)其他-认证&代理&超时配置
    python 接口自动化测试(四)cookie&session
    接口自动化测试 (三)request.post
    python 接口自动化测试二(request.get)
    python接口自动化测试(c测试环境的准备)
  • 原文地址:https://www.cnblogs.com/liyanbin/p/7459289.html
Copyright © 2011-2022 走看看