python的字典练习操作,包括遍历,取值操作。
''' Created on 2011-9-1 10<=value<=20 is E 20<value<=30 is P @author: WGY ''' dictInfor = [ {'id': 1001, 'value': '11'}, {'id': 1001, 'value': '22'}, {'id': 1001, 'value': '13'}, {'id': 1001, 'value': '14'}, {'id': 1001, 'value': '26'}, {'id': 1002, 'value': '25'}, {'id': 1002, 'value': '11'}, {'id': 1002, 'value': '32'}, {'id': 1002, 'value': '23'}, {'id': 1003, 'value': '13'}, {'id': 1003, 'value': '15'}, {'id': 1003, 'value': '17'}, {'id': 1003, 'value': '18'}, ] dictInfor1 = [ {'id': 1001, 'sex': 'nan','age':'12'}, {'id': 1002, 'sex': 'man','age':'23'}, {'id': 1003, 'sex': 'female','age':'43'}, {'id': 1004, 'sex': 'male','age':'43'}, {'id': 1005, 'sex': 'mao','age':'42'}, ] dict_e = {} dict_p = {} for items in dictInfor: e_list = [] if int(items['value']) >= 10 and int(items['value'])<= 20 : if items['id'] not in dict_e.keys(): dict_e[items['id']] = e_list e_list.append(items['value']) else: dict_e[items['id']].append(items['value']) p_list=[] if int(items['value'])>=20 and int(items['value']) <=30: if items['id'] not in dict_p.keys(): dict_p[items['id']] = p_list p_list.append(items['value']) else: dict_p[items['id']].append(items['value']) idList = [] for items in dict_e.keys(): if items in dict_p.keys(): idList.append(int(items)) for items in dictInfor1: if items['id'] in idList: print str(items['id'])+','+str(items['sex'])+','+str(items['age']) print dict_e print dict_p print idList