1 # dict 字典 2 dict = {'name':'kali','age':7,'class':'zero'} 3 4 # return {'name': 'kali', 'age': 7, 'class': 'zero'} <class 'dict'> 5 print(dict,type(dict)) 6 7 # return dict_items([('name', 'kali'), ('age', 7), ('class', 'zero')]) 8 print(dict.items()) 9 10 11 # return dict_keys(['name', 'age', 'class']) 12 print(dict.keys()) 13 14 # dict_values(['kali', 7, 'zero']) 15 print(dict.values()) 16 17 # print name age class 18 for x in dict.keys(): 19 print(x,end=' ') 20 print("") 21 22 # print kali 7 zero 23 for y in dict.values(): 24 print(y,end=' ') 25 print('') 26 27 # 字典转为字符串 return: {'name': 'kali', 'age': 7, 'class': 'zero'} <class 'str'> 28 print(str(dict),type(str(dict))) 29 30 # 字典可以转为元组 默认取字典中的key return : ('name', 'age', 'class') <class 'tuple'> 31 print(tuple(dict),type(tuple(dict))) 32 33 # 字典可以转为元组,return:('kali', 7, 'zero') 34 print(tuple(dict.values())) 35 36 # 字典转为列表 默认取字典中的key return: ['name', 'age', 'class'] <class 'list'> 37 print(list(dict),type(list(dict))) 38 # 字典转为列表 ['kali', 7, 'zero'] <class 'list'> 39 print(list(dict.values()),type(list(dict))) 40 41 # tuple 元组 42 tup=(1, 2, 3, 4, 5) 43 print(tup,type(tup)) 44 45 # 元组转为字符串,return:(1, 2, 3, 4, 5) 46 print(tup.__str__(),type(tup.__str__())) 47 # 元组转为列表 return: [1, 2, 3, 4, 5] <class 'list'> 48 print(list(tup),type(list(tup))) 49 50 # 元组不可以转为字典 列表类似元组, 元组与列表的区别是元组中的元素不可变 51 52 # str 字符串 53 54 # eval() 函数用来执行一个字符串表达式,并返回表达式的值。 55 # 字符串转为元组,return:(1, 2, 3) 56 print(tuple(eval("(1,2,3)"))) 57 # 字符串转为列表,return:[1, 2, 3] 58 print(list(eval("(1,2,3)"))) 59 # 字符串转为字典,return:<type 'dict'> 60 print(type(eval("{'name':'ljq', 'age':24}")))