forloop故障

2024-05-14 04:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,我的英语不是很好,所以我试着简短地解释一下, 这是我的密码:

self.boni_new = {}

for asdf in range(0,5):
    for i in xrange(0,5):
        vas = 5-i
        self.boni_new[asdf] = {}
        self.boni_new[asdf][5-i] = {}
        self.boni_new[asdf][5-i][0] = ui.TextLine()
        self.boni_new[asdf][5-i][0].SetParent(self)
        self.boni_new[asdf][5-i][0].SetText("Bonus "+str(5-asdf))
        self.boni_new[asdf][5-i][0].SetPosition(15,10+25*(vas-1))
        self.boni_new[asdf][5-i][0].Hide()
        self.boni_new[asdf][5-i][1] = DropDown(self,"- Keiner -")
        self.boni_new[asdf][5-i][1].SetPosition(70,10+25*(vas-1))
        for x in AFFECT_DICT:
            if x in BONI_AVAIL:
                    self.boni_new[asdf][5-i][1].AppendItem(str(AFFECT_DICT[x](0)),x)
        self.boni_new[asdf][5-i][1].SetSize(330,20)
        self.boni_new[asdf][5-i][1].Hide()
        self.boni_new[asdf][5-i][2] = ui.Bar("UI")
        self.boni_new[asdf][5-i][2].SetParent(self)
        self.boni_new[asdf][5-i][2].SetPosition(410,10+25*(vas-1))
        self.boni_new[asdf][5-i][2].SetColor(0xC0000000)
        self.boni_new[asdf][5-i][2].SetSize(80,20)
        self.boni_new[asdf][5-i][2].Hide()
        self.boni_new[asdf][5-i][3] = Edit2("0",14)
        self.boni_new[asdf][5-i][3].SetParent(self.boni_new[asdf][5-i][2])
        self.boni_new[asdf][5-i][3].SetNumberMode()
        self.boni_new[asdf][5-i][3].SetSize(80,20)
        self.boni_new[asdf][5-i][3].SetPosition(4,3)
        self.boni_new[asdf][5-i][3].Hide()
        dbg.TraceError(str(self.boni_new))

我想得到这个:

{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**5**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x052FC5F0>, 2: <ui.Bar object at 0x05301270>, 3: <switchbot.Edit2 object at 0x05301290>}}}
{0: {**2**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**2**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**2**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**2**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**4**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301290>, 2: <ui.Bar object at 0x05301ED0>, 3: <switchbot.Edit2 object at 0x05301EF0>}}}
{0: {**3**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**3**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**3**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**3**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**3**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301EF0>, 2: <ui.Bar object at 0x05309B50>, 3: <switchbot.Edit2 object at 0x05309B70>}}}
{0: {**4**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**4**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**4**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**4**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**2**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05309B70>, 2: <ui.Bar object at 0x0530F7D0>, 3: <switchbot.Edit2 object at 0x0530F7F0>}}}
{0: {**5**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**5**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**5**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**5**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**1**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x0530F7F0>, 2: <ui.Bar object at 0x0547D450>, 3: <switchbot.Edit2 object at 0x0547D470>}}}

但我明白了:

{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**5**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x052FC5F0>, 2: <ui.Bar object at 0x05301270>, 3: <switchbot.Edit2 object at 0x05301290>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**4**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301290>, 2: <ui.Bar object at 0x05301ED0>, 3: <switchbot.Edit2 object at 0x05301EF0>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**3**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301EF0>, 2: <ui.Bar object at 0x05309B50>, 3: <switchbot.Edit2 object at 0x05309B70>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**2**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05309B70>, 2: <ui.Bar object at 0x0530F7D0>, 3: <switchbot.Edit2 object at 0x0530F7F0>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**1**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x0530F7F0>, 2: <ui.Bar object at 0x0547D450>, 3: <switchbot.Edit2 object at 0x0547D470>}}}

我真的不知道哪里是我的问题,我希望有人能告诉我如何解决我的问题


Tags: inselfuinewobjectbaratasdf
1条回答
网友
1楼 · 发布于 2024-05-14 04:15:26

主要的问题是,每次通过内部循环都要用{}覆盖self.boni_new[asdf]。把那条线移到内环前面

您可以直接在“reverse”序列上迭代,而不是定义vas = 5 - i,因为您从不使用i

您还可以通过更好地使用临时变量来简化代码

self.boni_new = {}

for asdf in range(0,5):
    self.boni_new[asdf] = {}
    for vas in xrange(5,0,-1):
        d = self.boni_new[asdf][vas] = {}
        d[0] = ui.TextLine()
        d[0].SetParent(self)
        d[0].SetText("Bonus "+str(5-asdf))
        d[0].SetPosition(15,10+25*(vas-1))
        d[0].Hide()

        d[1] = DropDown(self,"- Keiner -")
        d[1].SetPosition(70,10+25*(vas-1))
        for x, f in AFFECT_DICT.iteritems():
            if x in BONI_AVAIL:
                d[1].AppendItem(str(f(0)), x)
        d[1].SetSize(330,20)
        d[1].Hide()

        d[2] = ui.Bar("UI")
        d[2].SetParent(self)
        d[2].SetPosition(410,10+25*(vas-1))
        d[2].SetColor(0xC0000000)
        d[2].SetSize(80,20)
        d[2].Hide()

        d[3] = Edit2("0",14)
        d[3].SetParent(self.boni_new[asdf][vas][2])
        d[3].SetNumberMode()
        d[3].SetSize(80,20)
        d[3].SetPosition(4,3)
        d[3].Hide()
        dbg.TraceError(str(self.boni_new))

相关问题 更多 >