Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Thursday, October 14, 2010

[Py] Assign values of one list to another

It is easy to ``copy'' lists in Python, but it is also easy to get things wrong, especially when you ignore something important like what I did.

Consider the following situation:
>>> A = [1,2,3]
>>> B = A
>>> B[0]=-1
>>> A
[-1, 2, 3]

What I want is to create a new list B which contains identical elements of the original list A. This doesn't work, however. As you can see in the above test, any modifications made on list B will affect list A. The reason is that when we type ``B=A'', the list B is just another name of list A. They are identical and of course are pointed to the same address. See the following tests:

>>> B is A
True
>>> B.index, A.index
(, )

So, if we need an independent list B which has a set of values' copy in list A, use ``list slicing'':
>>> B = A[:]
>>> B is A
False
>>> B.index, A.index
(, )
---
Ref: An Introduction to Python Lists

[Py] Be careful when create multi-dimensional lists

I wrote a note about the creation of multi-dimensional lists in Python, when I had not yet encountered another problem which have emerged recently. The problem is about appending items to the multi-dimensional lists.

Consider the following example:

>>> A = [[]]*3
>>> A
[[], [], []]
>>> for i in range(3):
...    for j in range(3):
...        A[i].append(i+j)
...
>>> A
[[0, 1, 2, 1, 2, 3, 2, 3, 4], [0, 1, 2, 1, 2, 3, 2, 3, 4], [0, 1, 2, 1, 2, 3, 2, 3, 4]]

But what I really want is something like
A = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

My guess is that the creation approach doesn't create a list which contains three independent rows, but just create three rows which actually point to the same address or something like that.

The solution (perhaps not the best one) is to create the rows in the form of list comprehension:

>>> A = [[] for rows in range(3)]
>>> A
[[], [], []]
>>> for i in range(3):
...    for j in range(3):
...        A[i].append(i+j)
...
>>> A
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Thursday, May 20, 2010

[Py] Multi-dimensional list

So far, the list in Python has bothered me a lot. I think it's because I do not understand it enough. These days I've been writing a little program to analyze some data, and encountered unexpected situations which almost got me down.

One unexpected situation was the multi-dimensional list, or the list of lists. The following page describes exactly what had troubled me:

(An Unofficial) Python FAQ Wiki (old link)
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
[[], [], []]