More random python observations from a perl programmer
Chad Netzer
chad at vision.arc.nasa.gov
Thu Aug 19 22:07:58 EDT 1999
Tom Christiansen wrote:
> You're right. I should have written
>
> new = []
> for i in range(0,len(old)):
> new[i] = old[i]
>
> And that certainly is parallel to the dictionary case. Dictionary
> auto-allocate. Lists don't.
Again, why not just use new.append(old[i])? If you REALLY want to
write it with an equals sign or start the indexing at other than zero, then
declare new as a dictionary (integers are valid keys since they are non
mutable)
old = [1,2,3,4,5,6,7,8,9]
new={}
for i in range(4, len(old)):
new[i] = old[i]
Autoallocating arrays, or arrays with holes, are simply Python dicts.
Of course, having something like new.sortedprint() might be nice,
but it is easy to define a function.
def sortedprint(dict):
spam = dict.keys()
spam.sort() # What, you've never heard of the amazing spamsort?
for i in spam:
print dict[i],
print
Chad Netzer
chad at vision.arc.nasa.gov
More information about the Python-list
mailing list