我真的不太确定如何在python中最好地接近以下内容...... 我知道我想要什么,并且知道我的想法,但我不知道我的想法是否是我正在寻找的最佳方法。
"Name":"path",contains[]
"Folder":"C:/blah/blah/folder",contains["file1","file2","file3"]
喜欢
things={}
things["Folder"]="C:/blah/blah/folder" AND contains["file1","file2","file3"]
以便它可以像:
一样使用for folder,path,contents in things.iteritems():
print("%s @ \"%s\" containing:\n\t")%(folder,path)
for file in contents:
print("%s\n\t")%(file)
如何将内容添加到内容中,例如
content.append(blah)
任何帮助将不胜感激!感谢。
答案 0 :(得分:1)
这样的东西?
>>> d = {}
>>> d["Folder"] = ["C:/blah/blah/Folder", ["file1","file2","file3"]]
>>> d["more"] = ["/home/mydir", ["file1","file2","file3"]]
>>> d["Folder"][0]
'C:/blah/blah/Folder'
>>> d["Folder"][1]
['file1', 'file2', 'file3']
>>> d["Folder"][1].append("file4")
>>> d["Folder"][1]
['file1', 'file2', 'file3', 'file4']
>>> for entry in d:
... d[entry][1].append("newfile")
...
>>> d
{'Folder': ['C:/blah/blah/Folder', ['file1', 'file2', 'file3', 'file4', 'newfile']],
'more': ['/home/mydir', ['file1', 'file2', 'file3', 'newfile']]}
答案 1 :(得分:0)
我认为你正在寻找一个嵌套字典:
things = {"Folder" : {"path" : "C:/blah/folder", "contents" : ["file1", "file2"]},
"Directory" : {"path" :"C:/foo/dir", "contents" : ["fileX", "fileY"]}}
for folder, info in things.iteritems():
print("%s @\"%s\" containing:\n\t")%(folder, info["path"])
for file in info["contents"]:
print("%s\n\t")%(file)
答案 2 :(得分:0)
things = {}
things["Folder"] = ("C:/blah/blah/folder", ["file1","file2","file3"])
for folder, (path, contents) in things.iteritems():
print("%s @ \"%s\" containing:\n\t")%(folder,path)
for file in contents:
print("%s\n\t")%(file)