Re: default value for list access?

2005-02-27 Thread Bo Peng
Sounds to me like the best solution is to simplify the implementation and dispense with the list alternative. Why use a list if a dictionary is suitable? Don't say performance: that's premature optimization. Dictionaries already have what you need, apparently, with setdefault(), so just use them

Re: default value for list access?

2005-02-27 Thread Ruslan Spivak
Ð ÐÑÐ, 27/02/2005 Ð 14:03 -0600, Bo Peng ÐÐÑÐÑ: > To clearify the problem: > > The data is the count of something, for example a[90]=10. a may be a > dictionary if the range is huge and a list when the range is reasonably > small. In the dictionary case, I can use a.setdefault(80, 0) if key 80

Re: default value for list access?

2005-02-27 Thread Peter Hansen
Bo Peng wrote: The data is the count of something, for example a[90]=10. a may be a dictionary if the range is huge and a list when the range is reasonably small. In the dictionary case, I can use a.setdefault(80, 0) if key 80 does not exist. In the list case, I have to check the length of list

Re: default value for list access?

2005-02-27 Thread Bo Peng
To clearify the problem: The data is the count of something, for example a[90]=10. a may be a dictionary if the range is huge and a list when the range is reasonably small. In the dictionary case, I can use a.setdefault(80, 0) if key 80 does not exist. In the list case, I have to check the lengt

default value for list access?

2005-02-27 Thread Bo Peng
Dear list, My program needs to do calculation based on a giving data structure (like a sparse matrix) with lots of missing values (invalid indices/keys of lists/dictionaries). The programing is difficult in that I have to use a try...except block for every item access. I have written a functio