Unble将元组列表转换为字典

2024-06-16 09:48:27 发布

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

我能得到元组,但不能转换成字典

for tag in list(tagged):
            w,p=tag
            print(w,p)

这是tuple的输出

    hi NN
    I PRP
    am VBP
    just RB
    testing VBG

它显示ValueError:执行此代码时没有足够的值来解包(预期值为2,实际值为1)

dicts={w:p for w,p in tag}
        print(dicts)

Tags: infor字典tagnnhiamlist
1条回答
网友
1楼 · 发布于 2024-06-16 09:48:27

这是为了说明tagged的结构应该是什么,这样它才能工作。我试过你的代码,它的工作。只是在创建dict时的一个变化

tagged = [('hi', 'NN') , ('I', 'PRP'), ('am','VBP'), ('just','RB'), ('testing','VBG')]

#for tag in list(tagged):
#            w,p=tag
#            print(w,p)

dict(tagged)

{'I': 'PRP', 'am': 'VBP', 'hi': 'NN', 'just': 'RB', 'testing': 'VBG'}

相关问题 更多 >