其他分享
首页 > 其他分享> > 字典创建列表的2种方法,作业

字典创建列表的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)

 

标签:setdefault,作业,列表,result,66,li,字典,append,row
来源: https://www.cnblogs.com/chengoudan/p/10430272.html