GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-datasructures-algorithms/PYTHON_NOTES.md at main · buwenzheng/python-datasructures-algorithms

Expand file tree

/

PYTHON_NOTES.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
51 lines (42 loc) · 2.59 KB
/

PYTHON_NOTES.md

Copy path

File metadata and controls

51 lines (42 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

JS → Python 迁移速查 + 踩坑记录

前端背景双修专用:所有「JS 里我会这么写,Python 怎么写」的记录都放这里。 算法练习只需要 Python 的「够用子集」,不要陷入全栈式学习。

一、语法对照(算法常用)

场景 JavaScript Python
数组 Array,push/pop list,append/pop;支持切片 a[1:3]、负索引
哈希表 Object / Map dict;安全取值 d.get(k, 0)
集合 Set set
队列 Array.shift()(O(n) 有坑) collections.deque + popleft()(O(1))
优先队列 需手写堆 heapq(最小堆;最大堆用负数入堆)
排序 arr.sort((a,b) => a-b) sorted(lst) / lst.sort()
记忆化 手写 memo 对象 functools.cache 装饰器
排列组合 手写回溯 itertools.permutations / combinations
二分插入/边界 手写 bisect.bisect_left / bisect_right
空值 null / undefined None(判断用 is None)
字符串模板 `${x}` f"{x}"
箭头函数 (x) => x * 2 lambda x: x * 2(仅单表达式)
数组变换 map / filter / reduce 列表推导式;sum(iterable, start)
变量交换 解构 [a, b] = [b, a] a, b = b, a
自增 i++ i += 1(没有 ++ 运算符)
相等比较 === ==(值相等);is 是身份比较
除法 /(浮点) / 恒为 float;整除用 //
无穷大 Infinity float("inf")
大整数 53 位精度限制 无溢出(自动大整数)

二、算法常用标准库(一次记牢)

from collections import deque, defaultdict, Counter
from heapq import heappush, heappop, heapify
from functools import cache
from bisect import bisect_left

三、高频踩坑(血泪区,持续追加)

  1. 空列表 / 空字典在 if 中为 False(JS 里 []、{} 都是 truthy)→ 判空写 if not arr:
  2. 与 None 比较用 is None,不要用 == None
  3. 二分中点:mid = (left + right) // 2,整除必须用 //
  4. 递归默认深度约 1000 → 深递归需要 import sys; sys.setrecursionlimit(10**6)
  5. 函数默认参数不要用可变对象(def f(a=[]) 是经典坑)
  6. dict 直接取不存在的 key 会 KeyError → 用 d.get(k, default) 或 defaultdict
  7. 闭包捕获循环变量是「迟到绑定」(循环里创建 lambda 需 lambda i=i: ... 固定)——JS 的 let 没有这个坑

四、学习过程中新发现(随时追加)

  • (在这里记录你在做题中真实踩过的坑)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.