List Behavior when inserting new items
mensanator at aol.com
mensanator at aol.com
Mon Jan 29 15:56:02 EST 2007
On Jan 29, 1:10 pm, "Drew" <olso... at gmail.com> wrote:
> > What is your actual usecase?
>
> > diezThe issue is that I don't know how long the list will eventually be.
> Essentially I'm trying to use a 2D list to hold lines that I will
> eventually print to the screen. Blank elements in the list will be
> printed as spaces. I suppose every time I add an element, I could find
> the difference between the size of the list and the desired index and
> fill in the range between with " " values, however I just wanted to
> see if there was a more natural way in the language.
I would use DBR's suggestion to use a dictionary and only store
actual values. In this example, I'm making a histogram that only
ends up having results for 4,7,8,9,10 but I want the graph to show
the zero occurrences as well, so I just create them on the fly
when printing.
print
print 'tc factor of 2 distribution (* scale = 8)'
print
tchistkeys = tchist.keys()
tchistkeys.sort()
prev_key = 0
for i in tchistkeys:
while prev_key<i:
print '%3d (%3d)' % (prev_key,0)
prev_key += 1
print '%3d (%3d)' % (i,tchist[i]),
hgraph = divmod(tchist[i],8)
s = '*'*(hgraph[0])
if hgraph[1]>0:
s = s + '.'
print s
prev_key += 1
## tc factor of 2 distribution (* scale = 8)
##
## 0 ( 0)
## 1 ( 0)
## 2 ( 0)
## 3 ( 0)
## 4 ( 1) .
## 5 ( 0)
## 6 ( 0)
## 7 (112) **************
## 8 (219) ***************************.
## 9 ( 58) *******.
## 10 (110) *************.
>
> Thanks,
> Drew
More information about the Python-list
mailing list