新手求助 字典类型转换成元组和列表
字典类型转换成元组和列表的时候只输出字典的键不输出字典的值吗 本帖最后由 昨非 于 2020-12-7 12:50 编辑默认是只取键的,不过可以把键和值单独取出来
a={'one':1,'two':2,'three':3}
t1=tuple(a.keys())
t2=tuple(a.values())
l1=list(a.keys())
l2=list(a.values())
print(t1,t2,l1,l2)
结果:('one', 'two', 'three') (1, 2, 3) ['one', 'two', 'three'] 直接转换么是这么规定的啊。你可以根据你的需要自己写代码转啊
a = {1:'one',2:'two'}
temp,temp2,b =[],[],[]
for each in a:
temp.append(each)
temp2.append(a)
b.append((each,a))
print(temp)
print(temp2)
print(b)
页:
[1]