8 字典的应用
1 1)应该习惯用此种方法 2 3 li = [11,22,33,44,55,77,88,99,90] 4 result = {} 5 for row in li: 6 if row > 66: 7 if 'k1'not in result: 8 result['k1'] = [] 9 result['k1'].append(row) 10 else: 11 if 'k2' not in result: 12 result['k2'] = [] 13 result['k2'].append(row) 14 print(result) 15 16 17 2)可以用此法达到同样效果 18 19 li = [11,22,33,44,55,77,88,99,90] 20 21 result = {} 22 23 for row in li: 24 result.setdefault('k1',[]) 25 result.setdefault('k2',[]) 26 if row > 66 : 27 result['k1'].append(row) 28 else: 29 result['k2'].append(row) 30 print(result)