One unexpected situation was the multi-dimensional list, or the list of lists. The following page describes exactly what had troubled me:
How do I create a multidimensional list?
This surprised me and made me thought again: ``Do I really know how to program in Python?'' The answer is definitely no. The low threshold to begin Python programming made me overestimate my understanding of it. Therefore, I told myself to learn more before stepping further.
---
Another situation surprised me is also about the list creation. The following test shows it:
>>> a = b = [[]]*3
>>> a[1] = 1
>>> b
[[], 1, []]
To assign values to list a also affects list b. To avoid this situation, you should create the lists separately.
>>> a = [[]]*3
>>> b = [[]]*3
>>> a[1] = 1
>>> b
[[], [], []]
No comments:
Post a Comment