加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:04:04 所属栏目:Python 来源:网络整理
导读:? ? class MonitoredKeywordMore(APIView): def post(self,request): try : # 设置原生命令并且请求数据 parents_asin = str(request.data. get ( " parentsasin " )) hour =str(request.data. hour )) # 执行原生mysql命令查询 raw_monitored_more = Monito

?

?

class MonitoredKeywordMore(APIView):
    
    def post(self,request):
        try:
            # 设置原生命令并且请求数据
            parents_asin = str(request.data.get("parentsasin"))
            hour=str(request.data.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 = obj.position
                    dict.(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[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  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] + 页第1] +  循环遍历数组 <结束>
                  console.log(this.keyWorkRanking",1)">.keyWorkRanking)
                })

              }
            }




控制台展示:
图在下面

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读