Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 PyGtk:从一个双重列表中形成一个树孔_Python 2.7_Pygtk - Fatal编程技术网

Python 2.7 PyGtk:从一个双重列表中形成一个树孔

Python 2.7 PyGtk:从一个双重列表中形成一个树孔,python-2.7,pygtk,Python 2.7,Pygtk,我正在尝试使用PyGtk从一个双重列表创建一个树存储。我就是这样做的 class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_size_request(400, 300) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.mai

我正在尝试使用PyGtk从一个双重列表创建一个树存储。我就是这样做的

class PyApp(gtk.Window): 
    def __init__(self):
        super(PyApp, self).__init__()

        self.set_size_request(400, 300)
        self.set_position(gtk.WIN_POS_CENTER)

        self.connect("destroy", gtk.main_quit)
        self.set_title("Tree")

        tree = gtk.TreeView()

        languages = gtk.TreeViewColumn()
        languages.set_title("Files")

        cell = gtk.CellRendererText()
        languages.pack_start(cell, True)
        languages.add_attribute(cell, "text", 0)

        treestore = gtk.TreeStore(str)

        doubleL = [['123', 'user1', 'Thisone' 'its.zip'],
        ['Kafueta', 'user1', 'zippedtto.zip'],
        ['Mohm', 'user1', 'zippedtto.zip'],
        ['Hom', 'user2', 'Iam.zip'], 
        ['abcd', 'test', 'Letsee.zip']]

        for lis in doubleL:
            par = treestore.append(None, [lis[0]])
            two = treestore.append(par, [lis[1]])
            treestore.append(two, [lis[2]])

        tree.append_column(languages)
        tree.set_model(treestore)

        self.add(tree)
        self.show_all()
这将生成一个树存储,可在GUI上显示,如下所示。但正如你所看到的,如果我有一个包含更多元素的列表,这不是很有效。 这个for循环可能非常长:

for lis in doubleL:
    par = treestore.append(None, [lis[0]])
    two = treestore.append(par, [lis[1]])
    treestore.append(two, [lis[2]]

在上面的for循环中,有没有更有效的方法来创建树

对于
reduce()
,这似乎是一个完美的例子:

在reduce中,上述lambda中的
x
基本上嵌套在前面的lambda中,使其与您的示例中的以下内容等效

treestore.append(treestore.append(treestore.append(None, [lis[0]]), [lis[1]]), [lis[2]])

添加内容确实是正确的,但是你可以通过应用另一篇文章中的技巧来提高效率:看看原来的文章,其中一个答案提供了另一个技巧。
treestore.append(treestore.append(treestore.append(None, [lis[0]]), [lis[1]]), [lis[2]])