首页 > 代码库 > 两种方法实现价格归类

两种方法实现价格归类

注意这里有个地方一定要加[]

 

# L1 = {11, 22, 33, 44, 55, 66, 77, 88, 99, 90}
# l1 = []
# l2 = []
# dic = ()
# for i in L1:
#     if i <= 66:
#         l1.append(i)
#     else:
#         l2.append(i)
# dic = ({‘k1‘:l1}, {‘k2‘:l2})
# print(dic)

L1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90, 133]
dic = {}
for i in L1:
    if i <= 66:
        if "k1" in dic.keys():
            dic[‘k1‘].append(i)
        else:
            dic[‘k1‘] = [i] #这里一定要加[],只有列表可以用append,int无法用append。
    else:
        if ‘k2‘ in dic.keys():
            dic[‘k2‘].append(i)
        else:
            dic["k2"] = [i]
print(dic)

  

两种方法实现价格归类