简单数据结构

  • 排列组合

    import itertools
    ​
    # 排列:从m个元素中提取n个,所有可能就是排列(有顺序)
    # 当m等于n时的排列称为全排列
    # it = itertools.permutations([1, 2, 3], 3)
    # 组合:没有顺序的排列
    # it = itertools.combinations([1, 2, 3, 4], 2)
    # 笛卡尔乘积:多个序列中的元素组合
    # it = itertools.product([1, 2], [3, 4], [5, 6])
    # 上面多个相同序列的场景
    it = itertools.product([1, 2], repeat=3)
    ​
    print(it)
    for i in it:
        print(i)
    ​
    # 可以转换为列表
    # print(list(it1))
  • 计数器及双向队列

    from collections import Counter, deque
    ​
    # 统计序列中元素出现的次数
    c = Counter([1, 2, 3, 4, 1, 2, 3, 1, 2, 1])
    ​
    print(c)
    print(type(c))
    # 可以转换为字典
    print(dict(c))
    ​
    # 双向队列
    d = deque([1, 2, 3])
    ​
    # 右侧追加
    d.append(4)
    # 左侧添加
    d.appendleft(5)
    ​
    # 右侧弹出数据
    print(d.pop())
    # 左侧弹出数据
    print(d.popleft())
    ​
    # 右侧扩展
    d.extend(['a', 'b', 'c'])
    # 左侧扩展
    d.extendleft(['aa', 'bb', 'cc'])
    ​
    # 循环移动:正数表示向右移动,负数表示向左移动
    # d.rotate(1)
    d.rotate(-1)
    print(d)
    print(list(d))
  • 链表

    • 添加节点

    • 追加节点

    • 插入节点

    • 删除节点

猜你喜欢

转载自www.cnblogs.com/542684416-qq/p/9807234.html
今日推荐