关于列表嵌套字典,我这个为什么报错?


def get_rainfall():
    a=input("type city name")
    total=[]
    while a!="":
        b=input("type rainfall in m")
        total.append({a:b})
        if b!="":
            a=input("type city name")
    print(total)

    for i in total:
        for j, k in i:
            print(f"{j}:{k}")

get_rainfall()

返回是:


type city namel
type rainfall in m5
type city namek
type rainfall in m6
type city name
[{'l': '5'}, {'k': '6'}]
Traceback (most recent call last):
 line 15, in <module>
    get_rainfall()
line 12, in get_rainfall
    for j, k in i:
ValueError: not enough values to unpack (expected 2, got 1)

很奇怪啊,明明列表打印没问题,为什么取有问题?哪里出错了?

i是字典吧,应该用 i.items() 不然只有key的

expected 2, got 1
期望两个参数 只给了一个。
好好调试排查一下。