元组到字典的 python 列表(多个值)

我想将元组列表转换为字典。我想转换这个:

a=[('ram',1,"us"),("shyam",2,"np")]

到:

a={"ram":(1,"us"),"shyam":(2,"np")}


慕容3067478
浏览 135回答 3
3回答

慕丝7291255

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

a=[('ram',1,"us"),("shyam",2,"np")]def convert(lst):    dicto = {}    for el in lst:        dicto[el[0]]= (el[1], el[2])    return dictoprint(convert(a))输出:{'ram': (1, 'us'), 'shyam': (2, 'np')}

慕哥6287543

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')} 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python