• 【python 数据结构】相同某个字段值的所有数据(整理成数组包字典的形式)


    class MonitoredKeywordMore(APIView):
        
        def post(self, request):
            try:
                # 设置原生命令并且请求数据
                parents_asin = str(request.data.get("parentsasin"))
                hour=str(request.data.get("hour"))
                # 执行原生mysql命令查询
                raw_monitored_more = MonitoredMore(hour, parents_asin)
                obj_rawqueryset = models.MonitoredGoods.objects.raw(raw_monitored_more)
    
                # 对请求数据进行序列化
                json_data = {}
                dict = {}
    
                for obj in obj_rawqueryset:
    
                    if not dict.get(obj.single_keyWord):  # 如果字典没有这个 关键词(key)
    
                        dict[obj.single_keyWord] = []  # 就以这个关键词 为key 创建一个空的数组(用于添加以这个关键词为key的rank信息) {'shoes': []}
    
                        # 创建一个字典添加这个关键词对应的所有排行
                        item = {}
                        item["heat"] = obj.heat
                        item["ranking"] = obj.ranking
                        item["position"] = obj.position
    
                        # 把这个关键词对对应的所有需要数据  做成一个字典放在组数里面
                        dict[obj.single_keyWord].append(item)
                    else:
                        item = {}
                        item["heat"] = obj.heat
                        item["ranking"] = obj.ranking
                        item["position"] = obj.position
                        dict.get(obj.single_keyWord).append(item)
    
                json_data['code'] = 200
                return Response(dict)
    
    
            except Exception as e:
    
                dict_error={}
                msg = traceback.format_exc()
                print(msg)
                logging_main.keyword_error.error(msg)
                dict_error['message'] = "请求异常"
                dict_error['code'] = 10004
                return JsonResponse(dict_error)

     前端页面的展示:

              // 请求数据
              expandGoodsContent(resent, this.parentsasin).then(response => {
                  if (response.code === 200) {
    
                    this.keyWorkRanking = []
                    response.data.some((item, i) => {
                      // 循环遍历数组 <开始>
                      for (var key in item) {
    
                        // console.log("this.keyWorkRanking", this.keyWorkRanking)
                        //
                        // console.log("键:", key);
                        // console.log("值:", item[key][item[key].length-1]);
                        //
                        // console.log("最后一个值:", item[key]);
                        // console.log("最后一个值的位置:",item[key][item[key].length-1].position.split(",")[1]);
    
    
                        this.keyWorkRanking.push({
    
                          keyword: key,
                          heat: item[key][item[key].length-1].heat,
                          rank: item[key][item[key].length-1].ranking + "," + "" + item[key][item[key].length-1].position.split(",")[0] + "页第" + item[key][item[key].length-1].position.split(",")[1] + "",
                        })
                      }
                      // 循环遍历数组 <结束>
                      console.log("this.keyWorkRanking",this.keyWorkRanking)
                    })
    
                  }
                }
    
    
    
    
    控制台展示:
    图在下面

  • 相关阅读:
    阿里云短信支付微信支付
    python 阿里云短信群发推送
    python twilio 短信群发 知识留存
    python爬虫 发送定时气象预报
    python beautifulsoup爬虫
    strongswan
    Docker进入主流,PaaS大有可为(转)
    Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)
    在IT网站上少花些时间
    Python 代码性能优化技巧(转)
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/12572086.html
Copyright © 2020-2023  润新知