在 Python 中限制柜台收款

Restricting a Counter Collection in Python

我目前正在从网络中提取数据 API 并且我正在尝试根据以下条件过滤一些值:计算用户在两个给定日期之间出现的次数但前提是 he/she 购买了商品 raspberry pi、banana pi 和 raspberry pi2 而不是商品 raspberry pi3

我收到的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])

我的方法是添加一些额外的 "condition" 来计算除项目 raspberry 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 我假设我收到此错误是因为集合计数器不允许我这样做。

我的问题是:

  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'])

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

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'])