1. 程式人生 > >Python學習筆記之基本數據結構方法

Python學習筆記之基本數據結構方法

ack 字典 訪問 mos span 函數返回 重復 空格 不存在

通用序列操作:

  • 索引,序列中元素從0開始遞增,這些元素可以通過編號訪問
  • 分片,使用索引只能訪問單個元素,分片操作可以訪問一定範圍內的元素。list[a:b]:a和b是兩個索引作為邊界,包含索引a對應函數,不包含b
  • 序列相加,兩種相同的序列才能進行連接操作
    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> a + b
    [1, 2, 3, 4, 5, 6]
    >>> ‘hello‘ + ‘world‘
    ‘helloworld‘

  • 乘法:用數字n乘一個序列會得到一個新的序列,新的序列中原來的序列將被重復n次。
    >>> a * 5
    [
    1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

    利用乘法和none可以對列表進行初始化

    >>> a = [None] * 10
    >>> a
    [None, None, None, None, None, None, None, None, None, None]
  • 成員資格in:檢查一個值是否在列表中
    >>> str = hello
    >>> h in str
    True
  • 長度(len)、最小值(min)和最大值(max)

list:

  • 元素賦值
    >>> x=[1,1,1]
    
    >>> x[1]=2 >>> x [1, 2, 1]
  • 刪除元素
    >>> del x[1]
    >>> x
    [1, 1]
  • 分片賦值,可用於替換,刪除,插入
    >>> x[:]=[4,5,6]
    >>> x
    [4, 5, 6]
  • 列表方法,append(以下都是列表方法):在列表末尾追加新的對象,直接修改原來的列表
    >>> x.append(5)
    >>> x
    [4, 5, 6, 5]
  • count:統計某個元素在列表中出現的次數
    >>> x.count(5)
    
    2
  • extend:在列表末尾一次性追加另一個序列中多個值
    >>> y = [1,2,3]
    >>> y.extend(x)
    >>> y
    [1, 2, 3, 4, 5, 6, 5]
  • index:找出某個值第一個匹配項的索引位置
    >>> a = [hello,python,world]
    >>> a.index(hello)
    0
  • insert:將對象插入到列表中
    >>> a.insert(0,hi)
    >>> a
    [hi, hello, python, world]
  • pop:移除列表中一個元素並返回該元素,默認最後一個
    >>> a
    [hi, hello, python, world]
    >>> a.pop()
    world
    >>> a.pop(1)
    hello
  • remove:用於移除列表中某個值的第一個匹配項,修改列表且沒有返回值
    >>> a
    [hi]
    >>> a.remove(hi)
    >>> a
    []
  • reverse:將列表中的元素反向存放
    >>> a = [1,3,4]
    >>> a.reverse()
    >>> a
    [4, 3, 1]
  • sort:對列表進行排序,無返回值,改變原列表內容,sorted函數返回一個排好序的列表
    >>> a
    [4, 3, 1]
    >>> a.sort()
    >>> a
    [1, 3, 4]

    sort方法有兩個可選參數,key和reverse,參數key提供一個在排序過程中使用的函數,這個函數為每一個元素創建一個鍵,所有元素根據鍵來排序。

    >>> x = [add,subl,hello,to]
    >>> x.sort(key = len)
    >>> x
    [to, add, subl, hello]

    reverse參數為true或是false,用來表明是否要進行反向排序

    >>> x.sort(key = len,reverse = True)
    >>> x
    [hello, subl, add, to]

元組:不可變序列

  • tuple函數:以一個序列作為參數,並把它轉換為元組
    >>> tuple(abc)
    (a, b, c)

字符串:

  • find方法:在一個較長的字符串中查找子串,返回子串所在位置最左端索引,沒有則返回-1。可提供匹配起點和結束點,包含起點不含終點。
    >>> hello world.find( )
    5

  • join方法:連接序列中的元素,被連接的序列元素要是字符串
    >>> str1=+
    >>> str2=[1,2,3]
    >>> str1.join(str2)
    1+2+3
    >>> str2.join(str1)
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        str2.join(str1)
    AttributeError: list object has no attribute join

  • replace:返回字符串所有匹配項被替換之後得到字符串
    >>> it\‘s a string.replace(s,r)
    "it‘r a rtring"

  • split:join的逆方法,將字符串分割成序列
    >>> 1/2/4/5/6.split(/)
    [1, 2, 4, 5, 6]

  • strip:去除兩側空格的字符串,也可以指定需要去除的參數
    >>>         hi        .strip()
    hi

    >>> ‘ !!!hi!!! ‘.strip(‘ !‘)
    ‘hi‘

  • translate:替換字符串中某些部分,只處理單個字符,可以同時進行多個替換
    >>> s = hello
    >>> table = s.maketrans(el,le)
    >>> s.translate(table)
    hleeo

字典:

  • clear:清除字典中所有項,無返回值
    >>> d
    {name: joy, age: 14}
    >>> d.clear()
    >>> d
    {}

  • copy:返回一個具有相同鍵-值對的新字典,淺復制
    >>> a = {name:admin,age:[1,2,3]}
    >>> b = a.copy()
    >>> b[name] = name
    >>> b
    {name: name, age: [1, 2, 3]}
    >>> a
    {name: admin, age: [1, 2, 3]}
    >>> b[age].remove(3)
    >>> a
    {name: admin, age: [1, 2]}
    >>> b
    {name: name, age: [1, 2]}

    深復制

    >>> from copy import deepcopy
    >>> c = deepcopy(b)
    >>> c
    {name: name, age: [1, 2]}
    >>> c[age].remove(1)
    >>> c
    {name: name, age: [2]}
    >>> b
    {name: name, age: [1, 2]}

  • get:在訪問不存在項時返回none
    >>> d = {name:a,age:7}
    >>> d.get(name)
    a
    >>> d.get(na)
    >>> 

  • items:
    >>> x = d.items()
    >>> x
    dict_items([(name, a), (age, 7)])
    >>> list(x)
    [(name, a), (age, 7)]

  • keys:返回針對鍵的叠代器
    >>> d.keys()
    dict_keys([name, age])

  • pop:
    >>> d.pop(age)
    7
    >>> d
    {name: a}
    >>> 

  • values:返回字典中的值

Python學習筆記之基本數據結構方法