zoukankan      html  css  js  c++  java
  • Python3基础——序列类型

     开头写给自己,To Myself:

    很久以来,都想要学习一门编程语言,从去年选择了python开始,反反复复重新开始了N多遍,每一次不会超过俩星期。昨天无意间翻开自己去年记的学习笔记,不禁感叹想当年我曾那么用功,却未能坚持下来,着实可惜。这一次,我告诉自己,最后一次机会,必须坚持到底!must。。。。。。

    Sequence Types — list, tuple, range

    Common Sequence Operations

    OperationResultNotes
    x in s True if an item of s is equal to x, else False——关系操作符 (1)
    x not in s False if an item of s is equal to x, else True——关系操作符 (1)
    s + t the concatenation of s and t——连接操作符 (6)(7)
    s * n or n * s equivalent to adding s to itself n times——重复操作符 (2)(7)
    s[i] ith item of s, origin 0——获取下标位i的元素,下标从0开始 (3)
    s[i:j] slice of s from i to j——切片拷贝 (3)(4)
    s[i:j:k] slice of s from i to j with step k (3)(5)
    len(s) length of s  
    min(s) smallest item of s  
    max(s) largest item of s  
    s.index(x[, i[, j]])

    index of the first occurrence of x in s (at or after index i and before index j)

    元素x在s中最早出现的位置下标,可以设置下标所在范围的起始位置

    (8)
    s.count(x) total number of occurrences of x in s——元素x在序列s中出现的次数  

    Immutable Sequence Types

    hash()

    Mutable Sequence Types

    OperationResultNotes
    s[i] = x item i of s is replaced by x  
    s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t  
    del s[i:j] same as s[i:j] = []  
    s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1)
    del s[i:j:k] removes the elements of s[i:j:k] from the list  
    s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x])  
    s.clear() removes all items from s (same as del s[:]) (5)
    s.copy() creates a shallow copy of s (same as s[:]) (5)
    s.extend(t) or s += t extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)  
    s *= n updates s with its contents repeated n times (6)
    s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x])  
    s.pop([i]) retrieves the item at i and also removes it from s (2)
    s.remove(x) remove the first item from s where s[i] == x (3)
    s.reverse() reverses the items of s in place (4)

    Lists

    1、创建列表的方法:

    • Using a pair of square brackets to denote the empty list: []
    • Using square brackets, separating items with commas: [a], [a, b, c]
    • Using a list comprehension: [x for x in iterable]
    • Using the type constructor: list() or list(iterable)

        list('abc') returns ['a', 'b', 'c']

        list( (1, 2, 3) ) returns [1, 2, 3]

    2、除了以上方法外,列表还支持sort()方法

      sort(*, key=None, reverse=None)

           对列表进行排序,默认是按照从小到大的顺序排列

    Tuples

    1、创建元组的方法:

    • Using a pair of parentheses to denote the empty tuple: ()
    • Using a trailing comma for a singleton tuple: a, or (a,)
    • Separating items with commas: a, b, c or (a, b, c)
    • Using the tuple() built-in: tuple() or tuple(iterable)

           For example, tuple('abc') returns ('a', 'b', 'c') and tuple( [1, 2, 3] ) returns (1, 2, 3)

    注意:当你要创建的元组只有一个元素时,必须带逗号

    Ranges

    代表一组不可变的数字序列,主要用于for循环

    class range(stop)

    class range(start, stop[, step])

    参数必须为整数,step步幅默认为1,start开始参数默认为0、

    >>> list(range(10))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(range(1, 11))
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list(range(0, 30, 5))
    [0, 5, 10, 15, 20, 25]
    >>> list(range(0, 10, 3))
    [0, 3, 6, 9]
    >>> list(range(0, -10, -1))
    [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
    >>> list(range(0))
    []
    >>> list(range(1, 0))
    []

    列表、元组、字符串总结
    共同点:
    都可以通过索引得到每一个元素
    默认索引值从0开始
    可以用通过分片得到一个范围内的元素的集合
    有很多共同的操作符(重复、拼接、成员关系操作符)

  • 相关阅读:
    HYSBZ 3813 奇数国
    HYSBZ 4419 发微博
    HYSBZ 1079 着色方案
    HYSBZ 3506 排序机械臂
    HYSBZ 3224 Tyvj 1728 普通平衡树
    Unity 3D,地形属性
    nginx 的naginx 种包含include关键字
    Redis 出现NOAUTH Authentication required解决方案
    mysql 8.0出现 Public Key Retrieval is not allowed
    修改jar包里的源码时候需要注意的问题
  • 原文地址:https://www.cnblogs.com/huluwahaha/p/7488048.html
Copyright © 2011-2022 走看看