Skip to content

Commit 61d7277

Browse files
author
暮晨
committed
EX.Deleting a list item while iterating
1 parent daeab88 commit 61d7277

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ So, here we go...
5757
- [Section: Watch out for the landmines!](#section-watch-out-for-the-landmines)
5858
- [> Modifying a dictionary while iterating over it/迭代字典时的修改](#-modifying-a-dictionary-while-iterating-over-it迭代字典时的修改)
5959
- [> Stubborn `del` operator/坚强的 `del` *](#-stubborn-del-operator坚强的-del-)
60-
- [> Deleting a list item while iterating](#-deleting-a-list-item-while-iterating)
60+
- [> Deleting a list item while iterating/迭代列表时删除元素](#-deleting-a-list-item-while-iterating迭代列表时删除元素)
6161
- [> Loop variables leaking out!](#-loop-variables-leaking-out)
6262
- [> Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)
6363
- [> Catching the Exceptions](#-catching-the-exceptions)
@@ -1420,7 +1420,7 @@ Deleted!
14201420
14211421
---
14221422
1423-
### > Deleting a list item while iterating
1423+
### > Deleting a list item while iterating/迭代列表时删除元素
14241424
14251425
```py
14261426
list_1 = [1, 2, 3, 4]
@@ -1453,30 +1453,30 @@ for idx, item in enumerate(list_4):
14531453
[2, 4]
14541454
```
14551455
1456-
Can you guess why the output is `[2, 4]`?
1456+
你能猜到为什么输出是 `[2, 4]`?
14571457
1458-
#### 💡 Explanation:
1458+
#### 💡 说明:
14591459
1460-
* It's never a good idea to change the object you're iterating over. The correct way to do so is to iterate over a copy of the object instead, and `list_3[:]` does just that.
1460+
* 在迭代时修改对象是一个很愚蠢的主意. 正确的做法是迭代对象的副本, `list_3[:]` 就是这么做的.
14611461
14621462
```py
14631463
>>> some_list = [1, 2, 3, 4]
14641464
>>> id(some_list)
14651465
139798789457608
1466-
>>> id(some_list[:]) # Notice that python creates new object for sliced list.
1466+
>>> id(some_list[:]) # 注意python为切片列表创建了新对象.
14671467
139798779601192
14681468
```
14691469
1470-
**Difference between `del`, `remove`, and `pop`:**
1471-
* `del var_name` just removes the binding of the `var_name` from the local or global namespace (That's why the `list_1` is unaffected).
1472-
* `remove` removes the first matching value, not a specific index, raises `ValueError` if the value is not found.
1473-
* `pop` removes the element at a specific index and returns it, raises `IndexError` if an invalid index is specified.
1470+
**`del`, `remove` `pop` 的不同:**
1471+
* `del var_name` 只是从本地或全局命名空间中删除了 `var_name` (这就是为什么 `list_1` 没有受到影响).
1472+
* `remove` 会删除第一个匹配到的指定值, 而不是特定的索引, 如果找不到值则抛出 `ValueError` 异常.
1473+
* `pop` 则会删除指定索引处的元素并返回它, 如果指定了无效的索引则抛出 `IndexError` 异常.
14741474
1475-
**Why the output is `[2, 4]`?**
1476-
- The list iteration is done index by index, and when we remove `1` from `list_2` or `list_4`, the contents of the lists are now `[2, 3, 4]`. The remaining elements are shifted down, i.e., `2` is at index 0, and `3` is at index 1. Since the next iteration is going to look at index 1 (which is the `3`), the `2` gets skipped entirely. A similar thing will happen with every alternate element in the list sequence.
1475+
**为什么输出是 `[2, 4]`?**
1476+
- 列表迭代是按索引进行的, 所以当我们从 `list_2` 或 `list_4` 中删除 `1` 时, 列表的内容就变成了 `[2, 3, 4]`. 剩余元素会依次位移, 也就是说, `2` 的索引会变为 0, `3` 会变为 1. 由于下一次迭代将获取索引为 1 的元素 (即 `3`), 因此 `2` 将被彻底的跳过. 类似的情况会交替发生在列表中的每个元素上.
14771477
1478-
* Refer to this StackOverflow [thread](https://stackoverflow.com/questions/45946228/what-happens-when-you-try-to-delete-a-list-element-while-iterating-over-it) explaining the example
1479-
* See also this nice StackOverflow [thread](https://stackoverflow.com/questions/45877614/how-to-change-all-the-dictionary-keys-in-a-for-loop-with-d-items) for a similar example related to dictionaries in Python.
1478+
* 参考这个StackOverflow的[回答](https://stackoverflow.com/questions/45946228/what-happens-when-you-try-to-delete-a-list-element-while-iterating-over-it)来解释这个例子
1479+
* 关于Python中字典的类似例子, 可以参考这个Stackoverflow的[回答](https://stackoverflow.com/questions/45877614/how-to-change-all-the-dictionary-keys-in-a-for-loop-with-d-items).
14801480
14811481
---
14821482

0 commit comments

Comments
 (0)