字典创建列表的2种方法,作业
li = [11,22,33,44,55,77,88,99,90]
大于66加入到一个ey,小于66加入到一个ey
li = [11,22,33,44,55,77,88,99,90] result = {} l1 = [] l2 = [] for row in li: if row > 66: l1.append(row) # result.setdefault('大于66',l1) else: l2.append(row) # result.setdefault('小于66',l2) result.setdefault('大于66',l1) result.setdefault('小于66',l2) print(result)
li = [11,22,33,44,55,77,88,99,90] result = {} for row in li: if row > 66: if '大于66' not in result: result['大于66'] = [] result['大于66'].append(row) else: if '小于66' not in result: result['小于66'] = [] result['小于66'].append(row) print(result)
li = [11,22,33,44,55,77,88,99,90] result = {} for row in li: result.setdefault('大于66',[]) result.setdefault('小于66',[]) if row > 66: result['大于66'].append(row) else: result['小于66'].append(row) print(result)