问题描述
下面是一个合理的通用方法,用于转换python
返回的元组/列表到dicts ......?我不是在提倡图书馆的功能
也会返回dicts(我可能会花更多时间查找真正的
名字...... :)我是只是想让我的代码更具可读性和
自我记录......
--------
class todict:
"""将列表/元组转换为dict:foo = todict(values,names)"""
def __init __(self,values,names):
self .d = {}
for i,name in enumerate(names.split(",")):
self.d [name.strip()] = values [i]
def __setitem __(self,name,value):
self.d [name] = value
def __getitem __(自我,姓名):
返回self.d [姓名]
导入os
#这种方式调用,我可以看到以什么顺序返回的内容
uname = todict(os.uname()," sysname,nodename,release,version,machine")
filename =" / etc / passwd"
osstat = todict(os.stat("%s"%filename),
" mode,inode ,dev,nlink,uid,gid,size,atime,mtime,cti me")
osstat [''filename''] =文件名
打印你在%(节点名)s上运行%(sysname)s%(release)s %uname
#mtime需要格式化...
print"文件''%(文件名)s''上次修改%(mtime)s" %osstat
---------
返回:
你在吉普赛人上运行Linux 2.6.3-15mdk .pfortin.com
档案''/ etc / passwd''上次修改时间为1080568025
设置了多个值但未使用;虽然我认为这是一个小的
价格,以使列表/元组的结果更具可读性和
有用......
随着时间的推移,我可能会建立一个我使用的功能列表,只需要
复制/粘贴我需要的行:
~ / pytemplates:
osstat = todict(os.stat("%s"%path),
" mode,inode,dev,nlink,uid ,gid,size,atime,mtime,cti me")
uname = todict(os.uname(),
" sysname,nodename,release,version,machine" ;)
等...
我唯一不喜欢的部分是:
osstat [''文件名''] =文件名
有没有办法重写类以允许调用parms的调用
包括生成的dict中的parms。 。?我可能也试着过来这里虽然很可爱但很可爱...:^)
新手谁能阅读(维护?)我的代码应该有一个更容易的
时间;至少,这是我的意图......虽然亚历克斯可能会考虑
这个样板...我想。一些。 boplaplating是好的... B-]
欢迎提出其他建议:^)
谢谢,
Pierre
Hi,
Is the following a reasonable generic approach for converting python
returned tuples/lists into dicts...? I''m not advocating library functions
also return dicts (I''d probably spend more time looking up the real
names... :) I''m just looking to make my code more readable and
self-documenting...
--------
class todict:
""" Converts a list/tuple to a dict: foo = todict(values,names) """
def __init__(self,values,names):
self.d = {}
for i,name in enumerate(names.split(",")):
self.d[name.strip()]=values[i]
def __setitem__(self, name, value):
self.d[name]=value
def __getitem__(self, name):
return self.d[name]
import os
# called this way, I can see what is returned in what order
uname = todict(os.uname(), "sysname, nodename,release,version, machine")
filename = "/etc/passwd"
osstat = todict(os.stat("%s" % filename),
"mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me")
osstat[''filename''] = filename
print "You''re running %(sysname)s %(release)s on %(nodename)s" % uname
# mtime needs formatting...
print "File ''%(filename)s'' was last modified %(mtime)s" % osstat
---------
Returns:
You''re running Linux 2.6.3-15mdk on gypsy.pfortin.com
File ''/etc/passwd'' was last modified 1080568025
A number of values are set and not used; though I think it''s a small
price to pay for making the results of lists/tuples more readable and
useful...
Over time, I''ll probably build a list of functions I use and just
copy/paste the line(s) I need:
~/pytemplates:
osstat = todict(os.stat("%s" % path),
"mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me")
uname = todict(os.uname(),
"sysname, nodename,release,version, machine")
etc...
The only part I still don''t like is:
osstat[''filename''] = filename
Is there a way to rewrite the class to allow for calls which take parms to
include the parms in the resulting dict..? I''m probably trying to get too
cute here though... :^)
The newbie who gets to read (maintain?) my code should have an easier
time; at least, that''s my intent... Though Alex will probably consider
this boilerplating... I think .some. boilerplating is good... B-]
Other suggestions welcome :^)
Thanks,
Pierre
推荐答案
使用
dict(zip( [" mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me"],
os.stat("%s"%path)))
-
问候,
Diez B. Roggisch
Use
dict(zip(["mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me"],
os.stat("%s" % path)))
--
Regards,
Diez B. Roggisch
[Diez B. Roggisch]使用
dict(zip([" mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me" ],
os.stat("%s"%path)))
[Diez B. Roggisch] Use
dict(zip(["mode,inode,dev,nlink,uid,gid,size,atime,mtime,cti me"],
os.stat("%s" % path)))
为什么要构建字典。使用os.stat提供的命名属性:
mode = os.stat(f).st_mode
这应该符合原始的可读性目标。
Raymond Hettinger
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.
Raymond Hettinger
[Diez B. Roggisch]
[Diez B. Roggisch]
什么zip()与这种情况有关...?
为什么要构建字典呢?使用
os.stat提供的命名属性:
模式= os.stat(f).st_mode
这应该符合原始的可读性目标。
What does zip() have to do with this situation...?
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.
那会教我使用已经命名属性的例子......:^)
我想要这样做一般来说 - 忽略那些名为
属性的少数;有很多没有我想要解决的问题...
目标是在我的
中指示的字符串映射键中使用dict原帖。
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.
这篇关于list / tuple to dict ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!