1 | initial version |
You can not create an entry of a list if it is not already defined.
So, this is not possible:
sage: L = []
sage: L[0] = 5
IndexError: list assignment index out of range
However, you can modify an existing element of a list:
sage: L = [1,2,3]
sage: L[1] = 5
sage: L
[1, 5, 3]
You can also add an element to a list:
sage: L = [1,2,3]
sage: L.append(5)
sage: L
[1, 2, 3, 5]
In particular, the set of indices of a list must be an interval of the form {0,1,2,...,n-1}
. Here, you will have a lot of holes. So, you probably need a dictionary instead of a list:
sage: D = dict()
sage: D[123] = 1
sage: D[12] = 42
sage: D
{12: 42, 123: 1}
sage: D[123]
1
2 | No.2 Revision |
You can not create an entry of a list if it is not already defined.
So, this is not possible:
sage: L = []
sage: L[0] = 5
IndexError: list assignment index out of range
However, you can modify an existing element of a list:
sage: L = [1,2,3]
sage: L[1] = 5
sage: L
[1, 5, 3]
You can also add an element to a list:
sage: L = [1,2,3]
sage: L.append(5)
sage: L
[1, 2, 3, 5]
In particular, the set of indices of a list must be an interval of the form {0,1,2,...,n-1}
. Here, you will have a lot In your case, the set of holes. indicdes is not contiguous. So, you probably need a dictionary instead of a list:
sage: D = dict()
sage: D[123] = 1
sage: D[12] = 42
sage: D
{12: 42, 123: 1}
sage: D[123]
1