美文网首页
python数据结构常用方法总结

python数据结构常用方法总结

作者: vvvillian | 来源:发表于2020-12-02 18:07 被阅读0次

    一、列表

    下面列举一下列表常用的自带方法
    1.1 list.append(x)
    1.2 list.remove(x)
    1.3 list.insert(i,x)
    1.4 list.pop(i)按索引移除, list.pop() 移除最后一个元素
    1.5 list.index(x[,start[,end]])
    1.6 list.clear() 删除列表所有元素
    1.7 del list 删除列表这个变量
    1.8 list.count(x)返回列表中x的个数
    1.9 list.sort(*, key=None, reverse=False) 改变列表本身
    1.10 list.reverse() 反转列表中的元素,改变列表本身
    1.11 list.copy() 返回列表的浅拷贝,相当于a[:]
    1.12 列表推导式

    [x**2 for x in range(10)]
    [(x, x**2) for x in range(10)]
    [ (x,y) for x in [1,2,3] for y in [3,1,4] if x!=y ]
    

    二、元组

    2.1 元组是不可变的,给元组中单独一个元素赋值是不允许的,但是如果其元素是列表等可变,则可以对该元素进行修改。如

    >>> t = 123, 456, [1,2,3], 'abc'
    >>> t[0] = 234
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        t[0] = 234
    TypeError: 'tuple' object does not support item assignment
    >>> t[2].append(4)
    >>> t
    (123, 456, [1, 2, 3, 4], 'abc')
    

    2.2 序列解包。序列解包要求等号左侧对变量数与右侧序列里所包含的元素数相同。如

    >>> t
    (123, 456, [1, 2, 3, 4], 'abc')
    >>> a,b,c,d = t
    >>> a
    123
    >>> b
    456
    >>> c
    [1, 2, 3, 4]
    >>> d
    'abc'
    

    三、集合

    集合是由不重复元素组成的无序的集。花括号或者set()都可以用来创建集合。
    注:要创建一个空集合只能用set(),{}是创建一个空的字典
    类似于列表,集合也支持推导式形式

    >>> s = {x for x in 'ejrljsdnvietohgnvdanw' if x not in 'abc'}
    >>> s
    {'g', 'v', 'l', 't', 'd', 'h', 's', 'r', 'i', 'o', 'j', 'n', 'w', 'e'}
    

    四、字典

    4.1 创建字典的方式

    # 使用花括号
    >>> d = {'jack':4098, 'sjoerd':2345}
    # 使用字典推导式
    >>> square = {x:x**2 for x in range(1,10)}
    >>> square
    {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
    # 使用类型构造器
    >>> dict([('jack',4098), ('sjoerd',2345)])
    {'jack': 4098, 'sjoerd': 2345}
    

    4.2 list(d)以列表的形式返回字典的键
    4.3 len(d)返回字典的项数
    4.4 d[key]
    4.5 key in d
    4.6 key not in d
    4.7 iter(d)返回以字典的键为元素的迭代器
    4.8 d.clear()
    4.9 d.copy()
    4.10 del d
    4.11 d.get(key)
    4.12 d.items()返回由字典项(键,值)组成的一个新视图

    >>> d1
    {'one': 1, 'two': 2, 'three': 3}
    >>> d1.items()
    dict_items([('one', 1), ('two', 2), ('three', 3)])
    

    4.13 d.keys()
    4.14 d.values()
    4.15 d.pop(key)从字典中移除并返回d[key]的值
    4.16 d.popitem()按后进先出的顺序移除并返回后进的键值对
    4.17 reversed(d)返回一个逆序获取字典键的迭代器。这是reversed(d.keys())的快捷方式。

    >>> d1
    {'two': 2, 'four': 4}
    >>> for key in d1:
        print(key)
    two
    four
    >>> for key in reversed(d1):
        print(key)  
    four
    two
    

    4.18 d.setdefault(key[,default]) 如果字典存在key,则返回它的值。如果不存在,则插入值为default的键key,并返回default

    >>> d1
    {'two': 2, 'four': 4}
    >>> d1.setdefault('three',3)
    3
    >>> d1
    {'two': 2, 'four': 4, 'three': 3}
    >>> d1.setdefault('two')
    2
    >>> d1
    {'two': 2, 'four': 4, 'three': 3}
    

    4.19 d.update(other)使用来自other的键值对更新字典,覆盖原有的键。返回None

    >>> d
    {'one': 1, 'two': 2}
    >>> d.update({'one':11,'three':3})
    >>> d
    {'one': 11, 'two': 2, 'three': 3}
    >>> 
    

    五、循环的技巧

    5.1 字典中循环

    >>> for k,v in d.items():
        pass
    

    5.2 序列中循环

    >>> a = ['cat','dog','tiger']
    >>> for i,v in enumerate(a):
        print(i,v)  
    0 cat
    1 dog
    2 tiger
    

    5.3 在两个或多个序列中循环时,可以用zip()函数将其内元素一一匹配

    >>> questions = ['name','guest','favorite color']
    >>> answers = ['lancelot', 'the holy qrail', 'blue']
    >>> for q,a in zip(questions,answers):
        print("What's your {0}? It's {1}.".format(q,a)) 
    What's your name? It's lancelot.
    What's your guest? It's the holy qrail.
    What's your favorite color? It's blue.
    

    5.4 逆向循环一个序列

    >>> for i in reversed(range(1,10,2)):
        print(i)
    9
    7
    5
    3
    1
    

    相关文章

      网友评论

          本文标题:python数据结构常用方法总结

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