华为机考前准备的一些比较杂的芝士

Python 中常见数据结构及用法总结

1. 列表 (list)

li = [1, 2, 3, 4, 5]

li.append(x) # 将x加入了列表

li.extend([1, 3]) # 利用可迭代对象扩展列表
li.extend((1, 3, 4))
li = li+[1, 3] # 将两个列表拼接并返回结果

li.pop() # 删除列表中最后一个元素,并返回该元素
li.pop(index) # 删除并返回给定索引处的值
del li[index] # 删除但不返回

li.remove(x) # 删除列表中的第一个x

li.insert(index, x) # 在给定索引处插入x

li.index(x) # 在整个列表范围内找首个x的索引
li.index(x, start) # 从索引start开始找首个x的索引
li.index(x, start, end) # 在索引区间[start, end)内找首个x的索引

li.count(x) # 返回列表中元素x的出现次数

li.sort() # 列表排序(in-place)
li = sorted(li)

li.reverse() # 列表翻转
li[::-1]

li.copy() # 列表的浅拷贝
li[:]

li.clear() # 清空列表
li = []
del li[:] # not del li

# 字符串列表拼接为一个字符串
li = ['hello', 'world']
' '.join(li) # ’‘内的字符为分隔符

python常用数据结构 - 算法杂货铺 (bjmsong.github.io)

19_python内置常用算法和数据结构 - Python 数据结构与算法视频教程 (pegasuswang.github.io)

collections — 容器数据类型 — Python 3.10.4 文档

5. 数据结构 — Python 3.10.4 文档

Sorted List — Sorted Containers 2.4.0 documentation (grantjenks.com)

(40条消息) Python中的bisect模块_Stephen__W的博客-CSDN博客_python中bisect

Python中bisect的使用方法 - python大师 - 博客园 (cnblogs.com)

Little and Big Endian Mystery - GeeksforGeeks

Queue in Python - GeeksforGeeks

[力扣1162] 多源BFS - 知乎 (zhihu.com)

694. 不同岛屿的数量 - 力扣(LeetCode) (leetcode-cn.com)

快速排序模板
import random
class Solution:
    def partition(self, nums, l, r):
        i, j = l, r
        idx = random.randint(i, j)  # 随机化选择pivot
        nums[i], nums[idx] = nums[idx], nums[i]
        while i < j:
            while i < j and nums[j] >= nums[l]: # <=
                j -= 1
            while i < j and nums[i] <= nums[l]: # >=
                i += 1
            nums[i], nums[j] = nums[j], nums[i]
        nums[i], nums[l] = nums[l], nums[i]
        return i

    def qsort(self, nums, l, r):
        if l < r:
            p = self.partition(nums, l, r)
            self.qsort(nums, l, p - 1)
            self.qsort(nums, p + 1, r)

    def sortArray(self, nums: List[int]) -> List[int]:
        self.qsort(nums, 0, len(nums) - 1)
        return nums
Python random 模块常见使用
random.random()  # 返回一个介于左闭右开[0.0, 1.0)区间的浮点数。
random.randint(a, b)  # 返回range[a,b]之间的一个整数。
random.uniform(a, b)  # 返回一个介于a和b之间(含a,b)的浮点数。如果a>b,则是b到a之间的浮点数。
random.randrange(start, stop[, step])  # 返回range[start,stop)之间的一个整数,可加步长step,跟range(0,10,2)类似。
Python 中交换两个数
a, b = b, a
Python 中小根堆用法总结
导入包
import heapq
常用方法
heapq.heapify(li) # 将一个列表 `in-place` 地转换为一个小根堆
heapq.heappush(li, item) # 将元素 item 加入堆中
heapq.heappop(li) # 从堆中弹出最小元素
heapq.heappushpop(li, item) # 相当于一次 push+pop
heapq.nlargest(n, li) # 返回一个长为n的列表,降序排列前n大元素
heapq.nsmallest(n, li) # 返回一个长为n的列表,升序排列前n小元素
将整数转为二进制形式
bin(n).replace('0b','')
Python 中集合(set)的有关操作
集合间的运算
a & b # 交集(intersection)
a | b # 并集(union)
a - b # 集合a中未在集合b出现的元素组成的集合 (difference)
a ^ b # 两个集合非公共元素组成的集合(symmetric_difference)
集合间的关系
a > b
a >= b # a.issuperset(b)
a < b
a <= b # a.issubset(b)
a.isdisjoint(b) # 交集为空?
元素与集合的关系
x in a
x not in a
Python 中格式化字符串
F 字符串

sort 之后报错 NoneType , sort 函数无返回值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值