0

我目前正在从 Web API 中提取数据,并且正在尝试根据以下条件过滤一些值:计算用户在两个给定日期之间出现的次数,但前提是他/她购买了树莓派、香蕉派等物品和树莓派2,而不是树莓派3

我收到的 JSON 对象具有以下结构:

[{
user_id : 0001
CreatedOn: "2017-02-16 15:54:48",
item: "raspbery pi",
VIP: "YES",
Vendor_CODE: "XYZ12345",
},
{
user_id : 0001
CreatedOn: "2017-02-15 13:49:16",
item: "raspbery pi2",
VIP: "YES",
Vendor_CODE: "XYZ67890",
},
{
user_id : 0001
CreatedOn: "2017-02-10 15:54:48",
item: "raspbery pi",
VIP: "YES",
Vendor_CODE: "RST171820",
},
{
user_id : 0001
CreatedOn: "2017-01-01 21:51:13",
item: "raspbery pi3",
VIP: "YES",
Vendor_CODE: "XOL002321",
},
{
user_id : 0005
CreatedOn: "2017-01-30 17:34:18",
item: "raspbery pi",
VIP: "YES",
Vendor_CODE: "RST171820",
},
{
user_id : 0005
CreatedOn: "2017-05-30 09:04:08",
item: "banana pi",
VIP: "YES",
Vendor_CODE: "ITI342027",
}]

目前我有以下代码计算用户在给定两个日期时出现的次数。

from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from collections import Counter
from datetime import datetime, timedelta
import json, urllib.request, dateutil.parser, urllib.parse,

#Request a response to the Web API
def get_data(request, *args, **kwargs):
    # YYYY-MM-DD
    start_date = datetime.now() - timedelta(days=7)
    end_date = datetime.now() - timedelta(days=1) 

    with urllib.request.urlopen("http://10.61.202.98:8081/T/ansdb/api/rows/dev/ect",timeout=15) as url:
    response_data = json.loads(url.read().decode())

    #count the number of times the user appears when he bought the 4 items in two given dates
    count_user_01 = Counter([k['user_id'] for k in response_data if
         start_date_week < dateutil.parser.parse(k.get('CreatedOn')) < end_date_week])

我的方法是添加一些额外的“条件”来计算除树莓派 pi3 之外的所有项目,例如:

count_user_01 = Counter([k['user_id'] for k in response_data if
     start_date_week < dateutil.parser.parse(k.get('CreatedOn')) < end_date_week] and k['item']!='raspberry pi3')

但是如果我这样做,那么我会遇到错误bool object is not iterable并且我假设我收到了这个错误,因为集合 Counter 不允许我这样做。

我的问题是:

  1. 我怎样才能在列表中实现这个额外的条件,以便我可以计算除 raspberry pi3 之外由一个用户购买的所有项目?

  2. 目前,我正在计算为特定用户购买的商品,但我如何计算所有用户购买的所有商品?

欢迎所有评论,答案和建议。

更新

对于问题 1,解决方案是在括号中解决问题。

count_user_01 = Counter([k['user_id'] for k in response_data if
 start_date_week < dateutil.parser.parse(k.get('CreatedOn')) < end_date_week and k['item']!='raspberry pi3'])
4

1 回答 1

1

我认为你的括号有问题。尝试这个:

count_user_01 = Counter([k['user_id'] for k in response_data if
     start_date_week < dateutil.parser.parse(k.get('CreatedOn')) < end_date_week and k['item']!='raspberry pi3'])
于 2017-06-07T18:58:20.650 回答