我想将元组列表转换为字典。我想转换这个:a=[('ram',1,"us"),("shyam",2,"np")]到:a={"ram":(1,"us"),"shyam":(2,"np")}
3 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
a=[('ram',1,"us"),("shyam",2,"np")]
d = {}
for e in a:
d[e[0]]= (e[1], e[2])
print(d)
输出:
{'ram': (1, 'us'), 'shyam': (2, 'np')}
慕娘9325324
TA贡献1783条经验 获得超4个赞
a=[('ram',1,"us"),("shyam",2,"np")]
def convert(lst):
dicto = {}
for el in lst:
dicto[el[0]]= (el[1], el[2])
return dicto
print(convert(a))
输出:
{'ram': (1, 'us'), 'shyam': (2, 'np')}
慕哥6287543
TA贡献1831条经验 获得超10个赞
def convert(lst):
return {tup[0]:(tup[1:]) for tup in lst }
a=[('ram',1,"us"),("shyam",2,"np")]
print(convert(a))
输出
{'ram': (1, 'us'), 'shyam': (2, 'np')}
添加回答
举报
0/150
提交
取消