美文网首页
python——序列

python——序列

作者: ELLENXX | 来源:发表于2019-07-05 20:06 被阅读0次

    迭代:重复反馈过程的活的,目的是达到所需要的目标和结果
    序列是可迭代的对象

    a=list()
    print(a)
    b='this is a apple'
    b=list(b)
    print(b)
    c=(1,2,3,4,5,6)
    c=list(c)
    print(c)
    

    输出
    []
    ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'a', 'p', 'p', 'l', 'e']
    [1, 2, 3, 4, 5, 6]

    过程:先新建一个列表,据索引迭代,将元素插入到列表中

    len():返回长度

    b='this is a apple'
    b=list(b)
    print(len(b))
    

    输出:15

    max():返回最大值

    b='this is a apple'
    b=list(b)
    print(max(b))
    

    输出:t
    还可以找元组,列表中的最大值

    print(max(1,2,3,4))
    

    输出:4

    min():返回最小值

    用法和max相似

    sum(iterable[,start=0]):返回序列iterable和可选参数start的总和

    tuple1=(2.1,3.2,4.1)
    print(sum(tuple1))
    

    输出:9.4

    tuple1=(2.1,3.2,4.1)
    print(sum(tuple1,1))
    

    输出:10.4

    sorted():排序

    tuple1=(5.1,3.2,4.1)
    print(sorted(tuple1))
    
    

    输出:[3.2, 4.1, 5.1]
    用sort()回报错

    reversed():逆转,返回一个对象需要用list将其变为list数组

    tuple1=(5.1,3.2,4.1)
    print(list(reversed(tuple1)))
    

    输出:[4.1, 3.2, 5.1]

    enumerate():将元素与索引组成元组

    tuple1=(5.1,3.2,4.1)
    print(list(enumerate(tuple1)))
    

    [(0, 5.1), (1, 3.2), (2, 4.1)]

    zip:返回有列表和另一个列表相同索引的元素组成元组

    list1=[1,2,3,4,5,6]
    list2=[2,1,3,4]
    print(list(zip(list1,list2)))
    

    输出:[(1, 2), (2, 1), (3, 3), (4, 4)]

    相关文章

      网友评论

          本文标题:python——序列

          本文链接:https://www.haomeiwen.com/subject/fokyhctx.html