迭代字典(不可迭代)和合并字典,python

时间:2017-10-13 09:33:04

标签: python python-3.x dictionary

首先,我对编程非常陌生,这是我关于编程的第一个问题。 我目前正在尝试在greenius中自动化模拟。我需要创建许多场景,然后替换其他参数。

我所有场景的字典应如下所示:

{'S1': 'a1, b1', 'S2': 'a1, b2', 'S3': 'a2, b1', 'S4': 'a2,b2', 'S5': 'a3,b1', 'S6': 'a3, b2'}

但如果我尝试以下代码,我就得不到我想要的结果。

def callback7():
  ax = int(SZ1) 
  bx = int(SZ2)
  for x in range(ax):
     Label(top, text='a'+str(x+1), relief=SUNKEN, width=5).grid(row=x, column=0)
     a.append(Entry(top, width=25))
     a[x].grid(row=x, column=1)
  for y in range(bx):
    Label(top, text='b'+str(y+1), relief=SUNKEN, width=5).grid(row=y, column=2)
    b.append(Entry(top, width=25))
    b[y].grid(row=y, column=3)
  B1 = Button(top, text='Save', width = 5,command=callback8).grid(row=0,column=4)

def callback8():
  for entry in a:
    Pa = entry.get()
    dcta['a{0}'.format(i)] = Pa
    i = i+1
  for entry1 in b:
    Pb = entry1.get()
    dctb['b{0}'.format(j)] = Pb
    j = j+1
  for key1, value1 in dcta.items:
    for key2, value2 in dctb.items:
        ar = value1
        br = value2
        dctz['S{0}'.format(u)] =  ar+'\t'+br
        u = u+1

我到这里的错误是:

TypeError: 'builtin_function_or_method' object is not iterable

我在这里做错了什么?

1 个答案:

答案 0 :(得分:-1)

更改代码的对齐方式。对齐是错误的。它应该如下。

def callback7():
    ax = int(SZ1) 
    bx = int(SZ2)
    for x in range(ax):
        Label(top, text='a'+str(x+1), relief=SUNKEN, width=5).grid(row=x, column=0)
        a.append(Entry(top, width=25))
        a[x].grid(row=x, column=1)
    for y in range(bx):
        Label(top, text='b'+str(y+1), relief=SUNKEN, width=5).grid(row=y, column=2)
        b.append(Entry(top, width=25))
        b[y].grid(row=y, column=3)
    B1 = Button(top, text='Save', width = 5,command=callback8).grid(row=0,column=4)

def callback8():

    for entry in a:
        Pa = entry.get()
        dcta['a{0}'.format(i)] = Pa
        i = i+1
    for entry1 in b:
        Pb = entry1.get()
        dctb['b{0}'.format(j)] = Pb
        j = j+1
    for key1, value1 in dcta.items:
        for key2, value2 in dctb.items:
            ar = value1
            br = value2
            dctz['S{0}'.format(u)] =  ar+'\t'+br
            u = u+1