[Tutor] Arrays and tuples

Gregor Lingl glingl@aon.at
Mon Jan 27 18:24:10 2003


Magnus Lycka schrieb:

> At 15:32 2003-01-27 +0000, Deirdre Hackett wrote:
>
>> I want to now put my 3 integer values into an array but it will not 
>> let me do
>>
>> data[0] = int(x2string)
>> data[1] = int(y2string).... etc
>
>
>   But while
>   l = []; l.append(x); l.append(y)
> is certainly a common construct, you can also initialize
> with values, which you tried to do...
>
>> instead I have to
>> data = (int(x2string), int(y2string), int(z2string))
>
Sometimes you may encounter a situation, where you know the length
of an array but NOT the order in which values will be put into that
array. In this situation you may use an approach similar to the
following one:

 >>> data = [None] * 4
 >>> data
[None, None, None, None]
 >>> data[2] = 4.25
 >>> data
[None, None, 4.25, None]
 >>> data[0] = 1
 >>> data
[1, None, 4.25, None]

Sincerely, Gregor