zoukankan      html  css  js  c++  java
  • 003 几个python编程例子

    作业

    1. 有如下值集合[11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中,结果为{'k1': 大于66的所有值, 'k2': 小于66的所有值}

      kema = {11 ,22 ,33 ,44 ,55 ,66 ,77 ,88 ,99 ,99 ,90}
      
      kee = {'k1':[] ,'k2':[]}
      for i in kema:
          if i> 66:
              kee['k1'].append(i)
      
          else:
              kee['k2'].append(i)
      print(kee)
      有难度
      
      第二种答案:自己搜集的答案
      li =[11,22,33,44,55,66,77,88,99,90]
      dic={}
      list_1 =[]  #大于66
      list_2 =[]  #小于66
      for i in li:
          if i ==66: continue
          if i > 66:
               list_1.append(i)
          else:
               list_2.append(i)
      dic.setdefault('k1',list_1)
      dic.setdefault('k2',list_2)
      print(dic)
      
    第三种答案:
    lis = [11,22,33,44,55,66,77,88,99,90]
    dic = dict()
    dic['k1'] = list()
    dic['k2'] = list()
    
    print(dic)
    
    for i in lis:
        if i > 66:
            dic['k1'].append(i)
        elif i < 66:
            dic['k2'].append(i)
            
    print(dic)
    
       
       
    1. 统计`s='hello jason tank tank jason sean say hello dsb dsb sb'`中每个单词的个数,结果为`{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}`。
    
       ```python
       s='hello jason tank tank jason sean say hello dsb dsb sb'
       s = s.split( )
       kee = {}
       for key in s:
           if key in kee:
               kee[key] += 1
           else:
               kee[key] = 1
       print(kee)
       
       
    
    1. 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
    pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
    
    linuxs={'wupeiqi','oldboy','gangdan'}
    
    1. 求出即报名python又报名linux课程的学员名字集合
    2. 求出所有报名的学生名字集合
    3. 求出只报名python课程的学员名字
    4. 求出没有同时这两门课程的学员名字集合
    
    print(pythons&linuxs) # print(f"{pythons.intersection(linuxs)}")
    print(pythons|linuxs) # print(f"{pythons.union(linuxs)}")
    print(pythons-linuxs) # print(f"{pythons.difference(linuxs)}")
    print(pythons^linuxs) # print(f"{pythons.symmetric_difference(linuxs)}")
    
    
    
    1. 有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
    l=[
        {'name':'egon','age':18,'sex':'male'},
        {'name':'alex','age':73,'sex':'male'},
        {'name':'egon','age':20,'sex':'female'},
        {'name':'egon','age':18,'sex':'male'},
        {'name':'egon','age':18,'sex':'male'},
    ]
    
    
    
    
    ```第一种答案:
    ln = []
    for i in l:
        if i not in ln:
            ln.append(i)
    
    print(ln)
    第二种答案:自己搜集
    
    new_stu_info_list = []
    for stu_info in stu_info_list:
        if stu_info not in new_stu_info_list:
            new_stu_info_list.append(stu_info)
    
    for new_stu_info in new_stu_info_list:
        print(new_stu_info)
    

    1.城市扁平列表转树状

    理解值引用和位置引用,实现一次遍历生成结果,时间复杂度为O(n)

    1.1

    输入:
    area_list = [{"code": "1", "parent_code": "0"},
                 {"code": "11", "parent_code": "1"},
                 {"code": "111", "parent_code": "11"},
                 {"code": "1111", "parent_code": "111"},
                 {"code": "11111", "parent_code": "1111"},
                 {"code": "111111", "parent_code": "111111"},
                 {"code": "2", "parent_code": "0"},
                 {"code": "22", "parent_code": "2"},
                 {"code": "222", "parent_code": "22"}]
    输出:
    result = {
        "sub":[
            {
                "code":"1",
                "sub":[
                    {
                        "code":"11",
                        "sub":[
                            {
                                "code":"111",
                                "sub":[
                                    {
                                        "code":"1111",
                                        "sub":[
                                            {
                                                "code":"11111",
                                                "sub":[
    
                                                ],
                                                "parent_code":"1111"
                                            }
                                        ],
                                        "parent_code":"111"
                                    }
                                ],
                                "parent_code":"11"
                            }
                        ],
                        "parent_code":"1"
                    }
                ],
                "parent_code":"0"
            },
            {
                "code":"2",
                "sub":[
                    {
                        "code":"22",
                        "sub":[
                            {
                                "code":"222",
                                "sub":[
    
                                ],
                                "parent_code":"22"
                            }
                        ],
                        "parent_code":"2"
                    }
                ],
                "parent_code":"0"
            }
        ]
    }
    
    解决方案:
    
    def solution(lists):
        result = {}
        for a in lists:
            a.setdefault("parent_code")
            result.setdefault(a['code'], {"sub": []})
            result.setdefault(a['parent_code'], {"sub": []})
            result[a['code']].update(a)
            result[a['parent_code']]['sub'].append(result[a['code']])
        return result["0"]
    

    1.2

    输入:

    industry_list = [
        {
            "pid": "女装",
            "id": "连衣裙"
        },
        {
            "id": "女装"
        },
        {
            "pid": "女装",
            "id": "半身裙"
        },
        {
            "pid": "女装",
            "id": "A字裙"
        },
        {
            "id": "数码"
        },
        {
            "pid": "数码",
            "id": "电脑配件"
        },
        {
            "pid": "电脑配件",
            "id": "内存"
        },
    ]
    

    输出:

    result = {
        "数码": {
            "电脑配件": {
                "内存": {}
            }
        },
        "女装": {
            "连衣裙": {},
            "半身裙": {},
            "A字裙": {}
        }
    }
    
    解决方案
    
    def solution(lists):
        data2 = {}
        for a in lists:
            a.setdefault("pid", "")
            data2.setdefault(a['id'], {})
            data2.setdefault(a['pid'], {})
            data2[a['pid']][a['id']] = data2[a['id']]
        return data2[""]
    

    **作业答案可参考:https://www.cnblogs.com/linhaifeng/articles/7133357.html#_label8`

  • 相关阅读:
    (*^_^*)Pose Estimation:DensePose
    (^_^)Pose Estimation:SamplePose
    [*v*/]人脸识别任务算法RetinaFace
    [*v*/]人脸识别任务算法MTCNN
    从Winograd算法看INT8量化及卷积加速原理
    [*_*/]Darknet && Mobilnet
    从TensorRT看INT8量化原理
    SSD算法精度
    信用评分卡模型的理论准备
    分类模型评估指标
  • 原文地址:https://www.cnblogs.com/abdm-989/p/11304741.html
Copyright © 2011-2022 走看看