| JS / TS | Python | 说明 |
|---|---|---|
let arr = [1,2,3] |
arr = [1, 2, 3] |
数组 = list |
for (let x of arr) |
for x in arr: |
for-in 语法简化 |
if (a > b) { ... } |
if a > b: |
没有括号、用冒号 |
function sum(a,b){} |
def sum(a, b): |
定义函数用 def |
return |
return |
相同 |
null |
None |
空值 |
true / false |
True / False |
首字母大写 |
dict = { "a": 1 } |
d = {"a": 1} |
字典 = 对象 |
arr.push(5) |
arr.append(5) |
添加元素 |
arr.pop() |
arr.pop() |
一样 |
len(arr) |
arr.length |
反着写,函数形式 |
console.log(x) |
print(x) |
输出 |
| 类型 | Python 对象 | 常用操作 |
|---|---|---|
| 数组 | list |
append(), pop(), len(), for ... in ... |
| 栈 | 用 list 模拟 |
stack.append(x) / stack.pop() |
| 队列 | collections.deque |
q.append(x) / q.popleft() |
| 集合 | set() |
add(), in, remove() |
| 字典 | dict() |
d[key] = val, for k,v in d.items() |
| 堆 | import heapq |
heapq.heappush(), heapq.heappop() |