dic = { 'name':'hema', 'age':40, 'wife':[{'name':'yeye','age':30},], 'children':{'girl':'ccc','boy':'ddd','secgirl':'eee'} } name = dic['name'] print(name) dic1 = dic['wife'] print(dic1) wife = dic1[0]['name'] print(wife) sec = dic['children']['secgirl'] print(sec) #列表转换字符串 users = ['aaa','bbb',666,'ccc'] users1 = [] for i in users: i = str(i) users1.append(i) s = '_'.join(users1) print(s) #元祖中的元素添加到另外一个列表 v1 = (11,22,33) v2 = [44,55,66] #v2.extend(v1) 一步搞定 for i in v1: v2.append(i) print(v2) #将元祖中的偶数索引的元素追加到列表v2 v1 = (11,22,33,44,55,66,77,88,99) v2 = [44,55,66] '''for i in range(len(v1)): 两种方法 if i % 2 == 0: v2.append(v1[i]) print(v2)''' v1 = v1 [2::2] v2.extend(v1) print(v2) #将字典的键值分别追加到两个列表中 key_list = [] value_list = [] info = {'aa':11,'bb':22,'cc':33,'dd':44} '''for i,j in info.items(): key_list.append(i) value_list.append(j)''' key_list.append(info.keys()) value_list.extend(info.values()) print(key_list) print(value_list) #字典的操作 dic = {'k1':'v1','k2':'v2','k3':[11,22,33]} for i in dic.keys(): print(i) for j in dic.values(): print(j) for x in dic.items(): print(x) dic['k4'] = 'v4' print(dic) dic['k1'] = 'alex' dic.get('k3').append(44) dic.get('k3').insert(0,18) print(dic) #嵌套字典操作 av = { '欧美':{ 'www.oumei.com':['aaaaaa','bbbbbb'], 'www.alex.com':['ccccc','ddddd'], 'tuple.com':['eeeee','fffff'], 'hao123.com':['ggggg','hhhhh'] }, '日韩':{ 'toyoka_hot':['iiiiii','jjjjjj'] }, '大陆':{ '1024':['mmmmm','nnnnn'] } } av.get('欧美').get('www.oumei.com').insert(1,'lianghenda') print(av) av.get('欧美').get('hao123.com').pop() print(av) s = av.get('日韩').get('toyoka_hot').pop() av.get('日韩').get('toyoka_hot').append(s.upper()) print(av) #av.get('大陆').update([('1048','yitianjiufengle')]) av.get('大陆').update({'1048':'yitianjiufengle'}) print(av) av.get('欧美').pop('tuple.com') print(av) av.get('大陆').get('1024')[0] = 'keyipaxialai' + av.get('大陆').get('1024')[0] print(av) #打印K2中值的各个元素 info = { 'k1':'v1', 'k2':[('alex'),('wupeiqi'),('oldboy')], } for i in info.get('k2'): print(i) #有字符串处理成字典 s = 'k:1|k1:2|k2:3|k3:4' dic = {} list1 = s.split('|') for i in list1: lis = i.split(':') #key,value = i.split(':') x = lis[0] y = lis[1] dic.setdefault(x,y) print(dic) #有如下值 li,将所有大于66的值保存到字典的第一个KEY内,小于的保存到第二个key li = [11,22,33,44,55,66,77,88,99,90] result = {'k1':[],'k2':[]} for i in li: if i <= 66: result.get('k2').append(i) else: result.get('k1').append(i) print(result) #输出商品列表,用户输入序号,现实用户选中的商品 goods = [ {'name':'电脑','price':1999}, {'name':'鼠标','price':10}, {'name':'游艇','price':20}, {'name':'美女','price':998} ] '''1,页面显示 序号 +商品名称 + 商品价格如: 1 电脑 1999 2 鼠标 10 2,用户输入选择的商品序号,然后打印商品名称及商品价格 3,如果用户输入的商品序号有误,则提示输入有误,并重新输入 4,用户输入Q或者q,退出程序 ''' '''for num,dic in enumerate(goods): print('{}\t{}\t{}'.format(num+1,dic['name'],dic['price'])) ''' count = 0 for i in goods: count = count + 1 print(str(count) + ' ' + i.get('name') + ' ' + str(i.get('price'))) while 1: s = input('请输入商品序号:(按Q退出程序)') cou = len(goods) if s.upper() == 'Q': print('退出程序') break if s.isdecimal(): if 0 < int(s) <= cou: print(goods[int(s)-1].get('name') + ' ' + str(goods[int(s)-1].get('price'))) else: print('超出范围') else: print('输入错误,请重新输入') #看代码写结果 v = {} for i in range(10): v['users'] = i print(v)