zoukankan      html  css  js  c++  java
  • 【Python练习】分类数据

    将11,22,33,44,55,77,66,88,99,9 进行分类,以66为界限
    代码1:
    ```
    list1 =[11,22,33,44,55,77,66,88,99,91]
    print(list1)
    list2 =[]
    list3 =[]
    for i in list1:
    if i<66:
    print(i)
    list2.append(i)
    else:
    list3.append(i)

    dict1 = {'k1':list2,'k2':list3}

    print(dict1['k1'])
    print(dict1['k2'])
    ```
    优化后代码:
    ```
    list1 =[11,22,33,44,55,77,66,88,99,91]
    dict1={}
    for i in list1:
    if i>66:
    if 'k1' in dict1.keys():
    dict1['k1'].append(i)
    else:
    dict1['k1']=[i,]
    else:
    if 'k2' in dict1.keys():
    dict1['k2'].append(i)
    else:
    dict1['k2'] = [i,]
    print("小于66的数值:{0}".format(dict1['k1']))
    print(dict1['k2'])
    ```




  • 相关阅读:
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    Zuul
    Turbine
    Hystrix
    Feign
    Ribbon
    Eureka
    @MappedSuperclass的作用
  • 原文地址:https://www.cnblogs.com/stellarilee/p/6059834.html
Copyright © 2011-2022 走看看