list/tuple to dict...

Pierre Fortin pfortin at pfortin.com
Thu Sep 16 16:10:35 CEST 2004


On Thu, 16 Sep 2004 09:24:16 +0200 Peter wrote:

> Pierre Fortin wrote:
> 
> >> > dict(zip(["mode,inode,dev,nlink,uid,gid,size,atime,mtime,ctime"],
> >> > os.stat("%s" % path)))
> > 
> > What does zip() have to do with this situation...?
> 
> It combines the items in the names list with the os.stat() result tuple
> to a list of (name, value) pairs that are then used to initialize the
> dictionary. For example:
> 
> >>> name_value_pairs = zip(["name1", "name2", "nameN"], ["va11", "val2",
> "valN"])
> >>> name_value_pairs
> [('name1', 'va11'), ('name2', 'val2'), ('nameN', 'valN')]
> >>> dict(name_value_pairs)
> {'nameN': 'valN', 'name2': 'val2', 'name1': 'va11'}

Ah...  your former example wasn't as obvious since "mode,inode...." was a
single string and produced different results...   Hadn't seen zip before
now and thought it was a compression thingy...  :>

 
> >> Why build the dictionary at all.  Use the named attributes provided
> >by> os.stat:
> >> 
> >>        mode = os.stat(f).st_mode
> >> 
> >> That should meet the original readability goals.
> > 
> > That'll teach me to use examples that already have named attributes...
> > :^) I'm looking to do this generically -- ignore the few that do have
> > named attributes; there are plenty without that I'd like to address...
> > 
> > The goal is to use the dict in string mapping keys as indicated in my
> > original post.
> 
> You can still do that while relying on the existing names:

True...  but again, my point is that this was a bad example...  I'm
looking to use the concept where there are no "existing names"

> Peter

Thanks,
Pierre



More information about the Python-list mailing list