Skip to content

Commit c701ffc

Browse files
author
暮晨
committed
EX.Modifying a dictionary while iterating over it
1 parent 9efc3b7 commit c701ffc

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ So, here we go...
5555
- [> Teleportation/空间移动 *](#-teleportation空间移动-)
5656
- [> Well, something is fishy.../嗯, 有些可疑...](#-well-something-is-fishy嗯有些可疑)
5757
- [Section: Watch out for the landmines!](#section-watch-out-for-the-landmines)
58-
- [> Modifying a dictionary while iterating over it](#-modifying-a-dictionary-while-iterating-over-it)
58+
- [> Modifying a dictionary while iterating over it/迭代字典时的修改](#-modifying-a-dictionary-while-iterating-over-it迭代字典时的修改)
5959
- [> Stubborn `del` operator *](#-stubborn-del-operator-)
6060
- [> Deleting a list item while iterating](#-deleting-a-list-item-while-iterating)
6161
- [> Loop variables leaking out!](#-loop-variables-leaking-out)
@@ -1341,7 +1341,7 @@ def square(x):
13411341
## Section: Watch out for the landmines!
13421342
13431343
1344-
### > Modifying a dictionary while iterating over it
1344+
### > Modifying a dictionary while iterating over it/迭代字典时的修改
13451345
13461346
```py
13471347
x = {0: None}
@@ -1365,14 +1365,14 @@ for i in x:
13651365
7
13661366
```
13671367
1368-
Yes, it runs for exactly **eight** times and stops.
1368+
是的, 它运行了**八次**然后才停下来.
13691369
1370-
#### 💡 Explanation:
1370+
#### 💡 说明:
13711371
1372-
* Iteration over a dictionary that you edit at the same time is not supported.
1373-
* It runs eight times because that's the point at which the dictionary resizes to hold more keys (we have eight deletion entries, so a resize is needed). This is actually an implementation detail.
1374-
* How deleted keys are handled and when the resize occurs might be different for different Python implementations.
1375-
* For more information, you may refer to this StackOverflow [thread](https://stackoverflow.com/questions/44763802/bug-in-python-dict) explaining a similar example in detail.
1372+
* Python不支持对字典进行迭代的同时修改它.
1373+
* 它之所以运行8次, 是因为字典会自动扩容以容纳更多键值(我们有8次删除记录, 因此需要扩容). 这实际上是一个实现细节. (译: 应该是因为字典的初始最小值是8, 扩容会导致散列表地址发生变化而中断循环.)
1374+
* 在不同的Python实现中删除键的处理方式以及调整大小的时间可能会有所不同.(译: 就是说什么时候扩容在不同版本中可能是不同的, 在3.6及3.7的版本中到[5](https://github.com/python/cpython/blob/v3.6.1/Objects/dictobject.c#L103-L110)就会自动扩容了. 以后也有可能再次发生变化. 顺带一提,后面两次扩容会扩展为32和256. 8->32->256)
1375+
* 更多的信息, 你可以参考这个StackOverflow的[回答](https://stackoverflow.com/questions/44763802/bug-in-python-dict), 它详细的解释一个类似的例子.
13761376
13771377
---
13781378

0 commit comments

Comments
 (0)