Python 09

作者: Jack_Hsin | 来源:发表于2017-12-15 19:53 被阅读0次

    一. 课上代码

    #删除元素的三种方法
    >>> member = ['fish', 'Jack', 'dark', 'David', 'road', 'quiet', 'sun']
    >>> member
    ['fish', 'Jack', 'dark', 'David', 'road', 'quiet', 'sun']
    >>> member.remove('David')
    >>> member
    ['fish', 'Jack', 'dark', 'road', 'quiet', 'sun']
    >>> member.remove('HsinChi')
    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in <module>
        member.remove('HsinChi')
    ValueError: list.remove(x): x not in list
    #remove()的话必须得知道数组中有哪个具体元素然后再进行删除
    
    >>> del member[1]
    >>> member
    ['fish', 'dark', 'road', 'quiet', 'sun']
    >>> del member
    >>> member
    Traceback (most recent call last):
      File "<pyshell#9>", line 1, in <module>
        member
    NameError: name 'member' is not defined
    #del 可以将整个数组删除,或者删除特定位置的元素
    
    >>> member = ['fish', 'Jack', 'dark', 'David', 'road', 'quiet', 'sun']
    >>> member.pop()
    'sun'
    >>> member
    ['fish', 'Jack', 'dark', 'David', 'road', 'quiet']
    >>> name = member.pop()
    >>> name
    'quiet'
    >>> member
    ['fish', 'Jack', 'dark', 'David', 'road']
    >>> member.pop(1)
    'Jack'
    #pop()可以删除特定位置的元素,或者直接删除最后一位的元素
    
    >>> member
    ['fish', 'dark', 'David', 'road']
    >>> member[1:3]
    ['dark', 'David']
    #这个是数组中的切片功能(slice)
    
    >>> member
    ['fish', 'dark', 'David', 'road']
    >>> member[:3]
    ['fish', 'dark', 'David']
    >>> member[1:]
    ['dark', 'David', 'road']
    >>> member[:]
    ['fish', 'dark', 'David', 'road']
    >>> member1 = member[:]
    >>> member1
    ['fish', 'dark', 'David', 'road']
    >>> 
    
    >>> list1 = [123]
    >>> list2 = [234]
    >>> list1 > list2
    False
    >>> list1 = [123, 456]
    >>> list2 = [234, 123]
    >>> list1 > list2
    False
    #此处进行比较,是选第一个元素进行比较,跟后面的元素没有关系
    
    >>> list3 = [123, 456]
    >>> (list1 < list2) and (list1 == list3))
    SyntaxError: invalid syntax
    >>> (list1 < list2) and (list1 == list3)
    True
    >>> list4 = list1 + list2
    >>> list4
    [123, 456, 234, 123]
    >>> list1 + 'Jack'
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        list1 + 'Jack'
    TypeError: can only concatenate list (not "str") to list
    #此处"+"的用处只能在都是数组的时候才能使用,如果有一方是数组另一方是元素那就不可以
    必须用extend()或者append()来对元素进行添加
    
    >>> list3
    [123, 456]
    >>> list3 * 3
    [123, 456, 123, 456, 123, 456]
    >>> list3 *= 3
    >>> lsit3
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        lsit3
    NameError: name 'lsit3' is not defined
    >>> list3
    [123, 456, 123, 456, 123, 456]
    >>> list3 *= 5
    >>> list3
    [123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
    >>> 123 in list3
    True
    >>> 'Jack' not in list3
    True
    >>> 123 not in list3
    False
    >>> list5 = [123, ['Jack', 'David'], 456]
    >>> 'Jack' in list5
    False
    >>> 'Jack' in list5[1]
    True
    >>> list5[1][1]
    'David'
    #此处选取David的方法比较重要
    
    >>> dir(list)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    >>> list3.count(123)
    15
    >>> list3.index(123)
    0
    >>> list3.index(123, 3, 7)
    4
    >>> list3.reverse()
    >>> list3
    [456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]
    >>> list6 = [4, 23, 6, 7, 3, 8, 43]
    >>> list6.sort()
    >>> list6
    [3, 4, 6, 7, 8, 23, 43]
    #sort()是从小到大排序
    
    >>> list6.sort(reverse = True)
    >>> list6
    [43, 23, 8, 7, 6, 4, 3]
    #list.sort(reverse = True)是从大到小排序
    
    >>> list7 = list6
    >>> list7
    [43, 23, 8, 7, 6, 4, 3]
    >>> list8 = list6[:]
    >>> list8
    [43, 23, 8, 7, 6, 4, 3]
    >>> list6.sort()
    >>> list7
    [3, 4, 6, 7, 8, 23, 43]
    >>> list8
    [43, 23, 8, 7, 6, 4, 3]
    #此处比较重要,需要多加小心
    >>> 
    

    二. 测试题

    1. list1 = [1, 3, 2, 9, 7, 8],请问list1[0]和list1[0:1]一样吗?
    不一样,list1[0]返回第0个元素的值,list1[0:1]返回一个只含有第0个元素的列表
    
    1. 如果你每次想从列表的末尾取出一个元素,并将这个元素插入到列表的最前边,你会怎么做?
    >>> list1 = [1, 3, 2, 9, 7, 8]
    >>> list1.insert(0, list1.pop())
    >>> list1
    [8, 1, 3, 2, 9, 7]
    
    1. 在进行分片的时候,我们知道分片的开始和结束位置需要进行指定,但其实还有另外一个隐藏的设置:步长。在普通分片操作中,步长默认为1,表示逐个遍历元素,也可以人为调整步长,如下所示:
    >>> list1 = [1, 3, 2, 9, 7, 8]
    >>> list1[0:6:2]
    [1, 2, 7]
    

    那么依你推测,关于步长的知识点还有哪些?

    >>> list1[::2]
    [1, 2, 7]
    #简洁分片操作
    
    >>> list1[::0]
    Traceback (most recent call last):
      File "<pyshell#6>", line 1, in <module>
        list1[::0]
    ValueError: slice step cannot be zero
    #步长不能是0,不然就走不动了
    
    >>> list1[::-1]
    [8, 7, 9, 2, 3, 1]
    >>> list1[::-2]
    [8, 9, 3]
    #步长可以是负数,改变方向(从尾部开始向左走)
    
    1. list1[-3:-1]可以得到[9, 7],这是为何?
    python的列表支持负数索引
    

    相关文章

      网友评论

        本文标题:Python 09

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