如何将 列表['1 one', '2 two', '3 three']
转化为 {1:'one',2:'two',3:'three'}
lst = ['1 one', '2 two', '3 three']
d = {}
for l in lst:
d[int(l.split(' ')[0])] = l.split(' ')[1]
print(d)
望采纳,谢谢!
a = ['1 one', '2 two', '3 three']
dic = {}
for i in a:
dic[i.split(' ')[0]] = i.split(' ')[1]
print(dic)
s= ['1 one', '2 two', '3 three']
p = {}
for l in s:
p[int(l.split(' ')[0])] = l.split(' ')[1]
print(p)
ls = ['1 one', '2 two', '3 three']
l1 = []
for i in ls:
l1.append(i.split()) # 先使用字符串方法分割成列表
print(l1)
dic = {}
for i in l1:
dic[int(i[0])] = i[1] # 再使用字典赋值的方式
print(dic)