Python基础篇(二)

     Python最基本的数据结构是序列(sequence),序列中的每个元素被分以以0开头的唯一的一个id号。

     Python中有6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和xrange对象。

     下面是一个使用列表的例子:

     >>> edward = ['Edward Gumby',42]
     >>> john = ['John Smith',50]
     >>> director = [edward,john]
     >>> director
     [['Edward Gumby', 42], ['John Smith', 50]]

     列表中的元素是可以变化的,例如可以在director中新添加一个对象。元组中的元素是不能变化的,当内部的元素不能被修改时,应该考虑使用元组。

    

    所有的序列类型都有以下的公共的操作:索引,分片,加,乘,检查是否存在某个元素,计算长度,找出最大最小元素,迭代。

    索引

    >>> greetings = "greetings"
    >>> greetings[-1]
    's'
    >>> greetings[0]
    'g'

    从左边开始使用索引是由0开始,从右边开始使用索引是由-1开始.

    分片   

    >>> numbers =[1,2,3,4,5,6,7,8,9]
    >>> numbers[3:6]
    [4, 5, 6]
    和Java的subString一样,第一个数字(3)是包含在内的,第二个数字(6)是不包含的。

    分片范围的第一个参数和第二个参数都是可以省略的,省略就表示开始或者结尾。    

    >>> numbers[-3:]
    [7, 8, 9]
    >>> numbers[:3]
    [1, 2, 3]

    实际上第一个参数和第二个参数都是可以省略的,如下:

    >>> numbers[:]

    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    也可以加上第三个参数,表示步长,隔几个数字生效。  

    >>> numbers[3:10:2]
    [4, 6, 8]

    步长也可以是负数,这时分片的方向是向前的。   

    >>> numbers[8:3:-1]
    [9, 8, 7, 6, 5]

        

    >>> [1,2,3]+[7,8,9]
    [1, 2, 3, 7, 8, 9]

    不同的类型是不能相加的,否则会出错:    

    >>> [1,2,3] +"hello"
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "str") to list

        

    >>> "python"*5
    'pythonpythonpythonpythonpython'
    >>> [12]*3
    [12, 12, 12]

    None表示列表什么都没有,但是却可以起到占位的作用,列表中的元素可以在后来在进行赋值。   

    >>> [None]*3
    [None, None, None]

    成员资格

    >>> permissions ="rw"
    >>> "r" in permissions
    True

    >>> user =['jack','mike','adas']
    >>> 'jack' in user
    True

    >>> database =[
    ... ['jack',34],
    ... ['mike',27],
    ... ['adas',30]
    ... ]
    >>> ['mike',27] in database
    True

    长度,最大值,最小值

     >>> numbers = [7,9,33]
     >>> len(numbers)
     3
     >>> max(numbers)
     33
     >>> min(4,7,22,3)
     3

     不同于元组和字符串,列表中的元素是可变的。使用list函数可将其他序列类型转换为列表。

     >>> list("hello")
     ['h', 'e', 'l', 'l', 'o']

     列表有一些其他序列类型不一定有的功能,例如:

     元素赋值

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

     删除元素

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

     分片赋值

     >>> name = list("Perl")
     >>> name
     ['P', 'e', 'r', 'l']
     >>> name[2:] = ['a','r']
     >>> name
     ['P', 'e', 'a', 'r']

     分片赋值可以在不改变原有元素的情况下插入新的元素:

     >>> numbers =[1,5]
     >>> numbers[1:1]=[2,3,4]
     >>> numbers
     [1, 2, 3, 4, 5]

     与上面同样的道理,分片赋值也可以进行元素的删除操作:

     >>> numbers
     [1, 2, 3, 4, 5]
     >>> numbers[1:4]=[]
     >>> numbers
     [1, 5]

     append方法

     append方法用于在列表的末尾增加新的元素,新增加的元素可以是新的类型:

     >>> mumbers = ["Jane","Mary","Tina"]
     >>> mumbers.append(1);
     >>> mumbers
     ['Jane', 'Mary', 'Tina', 1]

     extend方法

     extend方法可以将2个列表合称为1个列表:

     >>> a =[1,2,3]
     >>> b=[5,6,7]
     >>> a.extend(b)
     >>> a
     [1, 2, 3, 5, 6, 7]

     使用 a = a+b可以达到同样的效果。

     count方法

     count方法用于统计某个元素在列表中出现的次数:

     [1, 2, 3, 5, 6, 7]
     >>> ['to','be','or','not','to','be'].count('to')
     2

     index方法

     index方法用于找出匹配的元素在列表中的位置:

     >>> ['to','be','or','not','to','be'].index("be")
     1
     >>> ['to','be','or','not','to','be'].index("be",2)
     5
     >>> ['to','be','or','not','to','be'].index("be",2,4)
     Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     ValueError: 'be' is not in list

     insert方法

     insert方法用于将元素插入在指定的位置,用上述的分片功能也能完成:

     >>> numbers =[1,2,3,5,6,7]
     >>> numbers.insert(3,"four")
     >>> numbers
     [1, 2, 3, 'four', 5, 6, 7]

     pop方法

     pop方法返回列表中的元素并将其删除,默认返回的是最后一位的元素,也可以返回指定位置的元素

     >>> numbers = [1,2,3]
     >>> numbers.pop()
     3
     >>> numbers
     [1, 2]
     >>> numbers = [1,2,3]
     >>> numbers.pop(0)
     1
     >>> numbers
     [2, 3]

     remove方法

     remove方法用于移除列表中的元素,移除的只是第一次出现的元素

     >>> numbers = [1,2,3,1]
     >>> numbers.remove(1)
     >>> numbers
     [2, 3, 1]

     reverse方法

     >>> numbers = [1,2,3,1]
     >>> numbers.reverse()
     >>> numbers
     [1, 3, 2, 1]

     sort方法

     >>> numbers = [1,2,3,1]
     >>> y = numbers
     >>> y.sort()
     >>> y
     [1, 1, 2, 3]

    下面的做法是错误的,因为sort会对序列做排序操作,并返回了一个空值
    >>> numbers = [1,2,3,1]
    >>> y = numbers.sort()
    >>> y

   

原文地址:https://www.cnblogs.com/lnlvinso/p/3848489.html