Newbie lists question
Emile van Sebille
emile at fenx.com
Mon Aug 13 22:52:34 EDT 2001
Does something like this feel better?
>>> d = {}
>>> for i in (1,2,3,2,3,4,3,4,5):
d[i] = d.setdefault(i,[])
d[i].append(i)
HTH,
--
Emile van Sebille
emile at fenx.com
---------
"Wolfe Maykut" <wolfe at spam.me.not> wrote in message
news:3B785215.19D583C8 at spam.me.not...
> Hi. I'm trying to create a list on the end of a dictionary. I'd
> like to have a single loop, but since the dictionary is dynamically
> generated it appears as if I have to test to see if the key is around
> and then treat each case differently in order to initialize the loop.
> It looks ugly. Is there a nicer way of doing this that I'm missing?
>
> Thanks,
>
> --Wolfe
>
> #!/usr/bin/python
>
> import os, time, glob, string
>
> backupdir = glob.glob("/netbackups/.disks/disk*/*.out")
>
> prefixes = {}
>
> for file in backupdir:
> filename = string.split(file, '/')[-1]
> prefix = string.split(filename,'.')[0]
> if prefixes.has_key(prefix): # This is the loop
> I think is tres' ugly
> prefixes[prefix].append(file)
> else:
> prefixes[prefix] = []
> prefixes[prefix].append(file)
>
> for key in prefixes.keys():
> print key
> for file in prefixes[key]:
> st = os.stat(file)
> format = time.strftime("%b-%d-%Y-%H:00", time.localtime(st[8]))
> print " ", file, format
>
More information about the Python-list
mailing list