zoukankan      html  css  js  c++  java
  • map和filter函数

    大概用法如下:

    实际场景:

    # 需求:Meet算法推荐中,18岁以下(含18岁)的用户不出现meet卡片中,注册新账号,看下user_meet表的status
    from knight_util.MysqlUtil import MysqlPool
    from knight_util.get_db_token import tk_
    from knight_util.SendRequest import send_request
    from tqdm import tqdm
    mysql = MysqlPool()
    tk = tk_(2397).get('EPAL_WEB')
    
    
    def less_nineteen(num):
        """返回小于18的数据"""
        if num is None:
            pass
        else:
            return num < 18
    
    
    def get_userid_age_dict():
        """获取用户id和年龄"""
        headers = {
            "_tk_": tk,
            "content-type": "application/json",
            "user-agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64)"
        }
        data = {"limit": 30, "likeUserIds": [], "dislikeUserIds": [], "excludeUserIds": []}
        url = "https://test-community.gamesegirl.com/user/meets/match/v-3.6"
        response = send_request(url, headers, data)
        # print(response)
        if response['status'] == 'OK':
            response_length = len(response['content']['data'])
            # 接口返回的user_id和age
            user_info = {}
            for i in range(response_length):
                user_info.update(
                    {response['content']['data'][i]['userId']: response['content']['data'][i]['age']}
                )
            print(user_info)
            print(f"总共卡片数:{len(user_info.keys())}")
            null_value_list = [k for k, v in user_info.items() if v is None]
            print(null_value_list)
            # for k, v in user_info.items():
            #     if v is None:
            #         null_value_list.append(k)
            # print(null_value_list)
            # 小于18岁的年龄
            result = filter(less_nineteen, user_info.values())
            print(list(result))
    
    
    if __name__ == '__main__':
        get_userid_age_dict()

  • 相关阅读:
    Ruby on Rails中的Rake教程(Rake如何把我灌醉!)
    十一、Spring Boot 集成Shiro和CAS
    Spring Boot 快速入门
    一位创业者的血泪史----献给85前创业者的反思
    罗振宇送给新员工的四句话
    Axure 入门
    XMind常用快捷方式汇总
    CAS 单点登陆
    mysql互换表中两列数据
    mysql默认安装目录说明
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/13843636.html
Copyright © 2011-2022 走看看