Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit b16e58c

Browse files
update 03-notes
1 parent 2440bc0 commit b16e58c

File tree

1 file changed

+142
-1
lines changed

1 file changed

+142
-1
lines changed

03-dict-set/03-notes.ipynb

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,149 @@
286286
"\n",
287287
"这一切的背后其实都靠的是`__missing__`方法。\n",
288288
"\n",
289-
"### 3.4.2 `__missing__`方法\n"
289+
"### 3.4.2 `__missing__`方法\n",
290+
"\n",
291+
"\n",
292+
"## 3.7 不可变的映射类型 `types.MappingProxyType`\n",
293+
"> 如果给这个类一个映射,它会返回一个只读的映射视图。虽然是个只读视图,但是它是动态的。也就是说,如果对原映射做出了改动,我们通过这个视图可以观察到,但是无法通过这个视图对原映射做出改动。"
290294
]
295+
},
296+
{
297+
"cell_type": "code",
298+
"execution_count": 1,
299+
"metadata": {},
300+
"outputs": [
301+
{
302+
"data": {
303+
"text/plain": [
304+
"mappingproxy({1: 'A'})"
305+
]
306+
},
307+
"execution_count": 1,
308+
"metadata": {},
309+
"output_type": "execute_result"
310+
}
311+
],
312+
"source": [
313+
"from types import MappingProxyType\n",
314+
"d = {1:\"A\"}\n",
315+
"d_proxy = MappingProxyType(d)\n",
316+
"d_proxy"
317+
]
318+
},
319+
{
320+
"cell_type": "code",
321+
"execution_count": 2,
322+
"metadata": {},
323+
"outputs": [
324+
{
325+
"data": {
326+
"text/plain": [
327+
"'A'"
328+
]
329+
},
330+
"execution_count": 2,
331+
"metadata": {},
332+
"output_type": "execute_result"
333+
}
334+
],
335+
"source": [
336+
"d_proxy[1]"
337+
]
338+
},
339+
{
340+
"cell_type": "code",
341+
"execution_count": 3,
342+
"metadata": {},
343+
"outputs": [
344+
{
345+
"ename": "TypeError",
346+
"evalue": "'mappingproxy' object does not support item assignment",
347+
"output_type": "error",
348+
"traceback": [
349+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
350+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
351+
"Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43md_proxy\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
352+
"\u001b[1;31mTypeError\u001b[0m: 'mappingproxy' object does not support item assignment"
353+
]
354+
}
355+
],
356+
"source": [
357+
"d_proxy[2] = 'x'"
358+
]
359+
},
360+
{
361+
"cell_type": "code",
362+
"execution_count": 4,
363+
"metadata": {},
364+
"outputs": [
365+
{
366+
"data": {
367+
"text/plain": [
368+
"'B'"
369+
]
370+
},
371+
"execution_count": 4,
372+
"metadata": {},
373+
"output_type": "execute_result"
374+
}
375+
],
376+
"source": [
377+
"d[2] = 'B' \n",
378+
"d_proxy[2]"
379+
]
380+
},
381+
{
382+
"cell_type": "markdown",
383+
"metadata": {},
384+
"source": [
385+
"## 3.8 集合论\n",
386+
"\n",
387+
"### 3.8.1 集合字面量\n",
388+
"\n",
389+
"空集必须要用`set()`来表示,因为`{}`表示的是空字典。当时1330还错过\n",
390+
"\n",
391+
"> Python特性\n",
392+
"> 像{1, 2, 3} 这种字面量句法相比于构造方法(set([1, 2, 3]))要更快且更易读。后者的速度要慢一些,因为Python必须先从set这个名字来查询构造方法,然后新建一个列表,最后再把这个列表传入到构造方法里。但是如果是像{1, 2, 3}这样的字面量,Python会利用一个专门的叫作BUILD_SET的字节码来创建集合。\n",
393+
"\n",
394+
"`frozenset`没有字面量句法,只能通过构造方法来创建。\n",
395+
"\n",
396+
"### 3.8.2 集合推导\n",
397+
"和列表推导式类似,只是用`{}`来代替`[]`。\n",
398+
"\n",
399+
"### 3.8.3 集合的操作\n",
400+
"\n",
401+
"有一些之前不太熟悉的就地修改操作:\n",
402+
"- `s.update(t)` or `s |= t` union 就地修改\n",
403+
"- `s.intersection_update(t)` or `s &= t` intersection 就地修改\n",
404+
"- `s.difference_update(t)` or `s -= t` difference 就地修改\n",
405+
"- `s.symmetric_difference_update(t)` or `s ^= t` symmetric difference 就地修改\n",
406+
"\n",
407+
"这里传入的参数可以是任何可迭代对象,包括集合,列表,生成器等(一个有趣的特性)\n",
408+
"\n",
409+
"返回值为bool的一些运算符:\n",
410+
"- `s.isdisjoint(t)` 如果两个集合的交集为空,返回True\n",
411+
"- `s.issubset(t)` 如果s中的每一个元素都在t中,返回True\n",
412+
"- `s.issuperset(t)` 如果t中的每一个元素都在s中,返回True\n",
413+
"\n",
414+
"其他实用性方法:\n",
415+
"- `s.add(e)` 添加元素\n",
416+
"- `s.clear()` 清空集合\n",
417+
"- `s.remove(e)` 删除元素,如果不存在会报错\n",
418+
"- `s.discard(e)` 删除元素,如果不存在不会报错\n",
419+
"- `s.copy()` 返回一个新的集合(浅拷贝)\n",
420+
"- `s.pop()` 随机删除一个元素并返回\n",
421+
"\n",
422+
"## 3.9 dict和set的背后\n",
423+
"\n"
424+
]
425+
},
426+
{
427+
"cell_type": "code",
428+
"execution_count": null,
429+
"metadata": {},
430+
"outputs": [],
431+
"source": []
291432
}
292433
],
293434
"metadata": {

0 commit comments

Comments
 (0)