使用来自treeview Python的行使用for循环创建嵌套列表

问题描述 投票:0回答:1

我想使用从treeview获取的行中的值来创建嵌套列表。我想得到这样的东西:

[[ , , ], [ , , ], [ , , ]...]

嵌套列表的数量应该等于树视图中的行数,每个嵌套列表应该有5个项目。但是我的程序继续将所有行的所有值放到一个嵌套列表中,嵌套列表的数量等于行数。我该如何解决这个问题。

ListOnePar=[]
ListTwoPar=[]

for child in tree1.get_children(id1):

    one=round(float(tree1.item(child,"values")[1]),2)
    two=round(float(tree1.item(child,"values")[2]),2)
    tree=round(float(tree1.item(child,"values")[3]),2)
    four=round(float(tree1.item(child,"values")[5]),1)
    five=round(float(tree1.item(child,"values")[6]),1)

    ListTwoPar.append(one)
    ListTwoPar.append(two)
    ListTwoPar.append(tree)
    ListTwoPar.append(four)
    ListTwoPar.append(five)

    ListOnePar.append(ListTwoPar)

print(ListOnePar)
python
1个回答
1
投票

你需要在循环中坚持第二个列表创建;否则,它将继续追加到每一行的相同列表。

for child in tree1.get_children(id1):
    ListTwoPar=[]
© www.soinside.com 2019 - 2024. All rights reserved.