[Tutor] List of ints

Alan Gauld alan.gauld at btinternet.com
Tue Mar 3 10:34:45 CET 2015


On 03/03/15 06:50, Phil wrote:

> I'd like to set up a two dimensional list of counters as follows;
>
> count = [ [0], [0], [0] ]
>
> And then increment the first counter as follows;
>
> count [0] += 1

Are you trying to increment the zero to make it 1?
Or are you trying to add a new value, 1, to the first sublist?
ie Do you want the output to be:

count = [ [1], [0], [0] ]

OR

count = [ [0,1], [0], [0] ]

To do the first you need to increment the *value* of
the first sublist not the sublist:

count[0][0] += 1

To do the second you must use append():

count[0].append(1)

> The array module looks like the answer because it seems to function in
> the same way as an array under C.

The array module is pretty specialized, in most cases a simple
list is a better solution.

> Is there a way to add a value to a list of ints?

Yes, see above.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list