0

任何人都可以帮助我如何用日期遍历字典吗,我有这样的数据集

data=[{u'a': u'D', u'b': 100.0, u'c': 201L, u'd': datetime.datetime(2007, 12, 29, 0, 0), u'e': datetime.datetime(2008, 1, 1, 6, 27, 41)},
      {u'a': u'W', u'b': 100.0, u'c': 201L, u'd': datetime.datetime(2007, 12, 29, 0, 0), u'e': datetime.datetime(2008, 2, 4, 6, 27, 41)},
      {u'a': u'W', u'b': 100.0, u'c': 202L, u'd': datetime.datetime(2007, 12, 30, 0, 0), u'e': datetime.datetime(2008, 1, 1, 4, 20, 44)},
      {u'a': u'D', u'b': 100.0, u'c': 202L, u'd': datetime.datetime(2007, 12, 30, 0, 0), u'e': datetime.datetime(2008, 3, 11, 6, 27, 41)},
      {u'a': u'D', u'b': 100.0, u'c': 202L, u'd': datetime.datetime(2007, 12, 30, 0, 0), u'e': datetime.datetime(2008, 5, 8, 11, 2, 41)},
      {u'a': u'D', u'b': 100.0, u'c': 203L, u'd': datetime.datetime(2008, 1, 2, 0, 0), u'e': datetime.datetime(2008, 6, 1, 6, 27, 41)},
      {u'a': u'W', u'b': 100.0, u'c': 204L, u'd': datetime.datetime(2008, 2, 9, 0, 0), u'e': datetime.datetime(2008, 4, 21, 12, 30, 51)},
      {u'a': u'D', u'b': 100.0, u'c': 204L, u'd': datetime.datetime(2008, 2, 9, 0, 0), u'e': datetime.datetime(2008, 8, 15, 15, 45, 10)}]

我怎样才能把它带入以下格式的字典

res={u'201L':(1,0,1),(2,1,0),(3,0,0),(4,0,0).. so on till (12,0,0),
u'202L':(1,1,0),(2,0,0),(3,0,1),(4,0,0),(5,0,1)...(12,0,0),
u'203L':(1,0,0),(2,0,0),(3,0,0),(4,0,0),(5,1,0)...(12,0,0),
u'204L':(1,0,0),(2,0,0),(3,0,0),(4,1,0),(5,0,0),(6,0,0,(7,0,0),(8,0,1)...(12,0,0)}

其中 1, 2, 3 是从他们的卡发行日期算起的第一个月、第二个月等等,即201L发行日期是datetime.datetime(2007, 12, 29, 0, 0)202L它是 datetime.datetime(2007, 12, 30, 0, 0)

第一个月的意思是从2007-12-292008-1-29

  (1,0,1)---where 1 is the first month
  0 is no of times W
  1 is no of times D

我尝试过这样的事情

data_dict=defaultdict(Counter)
date_dic={}
for x in data:
  a,b,c,d=x['a'],x['c'],x['d'],x['e']
  data_dict[b][a] += 1
for key , value in data_dict.items():
   date_dic[key] = tuple(map(datetime.date.isoformat, (c,d)))
   for value in range(1,30):
      if value not x: continue

在 if 循环之后我被卡住了,我可以添加什么来获得上述格式。我最终得到了这样的东西作为我的输出,

defaultdict(<class 'collections.Counter'>, {201L: Counter({u'D': 1, u'W': 1}), 202L: Counter({u'D': 2, u'W': 1}), 203L: Counter({u'D': 1}), 204L: Counter({u'D': 1, u'W': 1})})
4

1 回答 1

2

我会创建一个日期列表,然后从该列表中找到将每个项目放入的“桶”。

datetime.timedelta()您可以使用对象从起点创建相对的新日期:

startdate = data[0]['d']
buckets = [startdate + datetime.timedelta(days=30) * i for i in xrange(12)]

现在您有 12 个日期来比较其他所有内容,因此您知道将每个后续值放入哪个存储桶:

>>> buckets
[datetime.datetime(2007, 12, 29, 0, 0), datetime.datetime(2008, 1, 28, 0, 0), datetime.datetime(2008, 2, 27, 0, 0), datetime.datetime(2008, 3, 28, 0, 0), datetime.datetime(2008, 4, 27, 0, 0), datetime.datetime(2008, 5, 27, 0, 0), datetime.datetime(2008, 6, 26, 0, 0), datetime.datetime(2008, 7, 26, 0, 0), datetime.datetime(2008, 8, 25, 0, 0), datetime.datetime(2008, 9, 24, 0, 0), datetime.datetime(2008, 10, 24, 0, 0), datetime.datetime(2008, 11, 23, 0, 0)]

然后我们可以使用该bisect模块找到匹配的桶:

from bisect import bisect

bisect(buckets, somedate) - 1  # Returns a value from 0 - 11

我们为每个用户创建这样的存储桶,因此我们需要在单独的映射中跟踪存储桶。实际上,我们将根据需要即时创建存储桶以适应当前交易日期。

接下来,我们使用一个collections.defaultdict实例来跟踪每个键的计数(c输入您的输入):

from collections import defaultdict

res = defaultdict(list)
empty_counts = {'D': 0, 'W': 0}

这将为您的存储桶创建一个列表,并为存款和取款创建一个空的计数字典。我在这里使用了字典,因为它比以后必须操作(不可变的)元组容易使用。我也没有包括月份编号(1 - 12);没意义,您已经为每个存储桶(0 - 11)建立了一个索引,并且您可以拥有可变数量的存储桶。

我们需要根据需要创建存储桶和计数器以适应当前日期;我们不是通过扫描数据来查找每个用户的最大交易日期,而是根据需要扩展我们的存储桶和计数列表:

def expand_buckets(buckets, bucket_counts, start, transaction):
    # This function modifies the buckets and bucket_counts lists in-place
    if not buckets:
        # initialize the lists
        buckets.append(start)
        bucket_counts.append(dict(empty_counts))

    # keep adding 30-day spans until we can fit the transaction date
    while buckets[-1] + datetime.timedelta(days=30) < transaction:
        buckets.append(buckets[-1] + datetime.timedelta(days=30))
        bucket_counts.append(dict(empty_counts))

现在我们可以开始数了:

per_user_buckets = defaultdict(list)

for entry in data:
    user = entry['c']
    type = entry['a']
    transaction_date = entry['e']
    buckets = per_user_buckets[user]
    bucket_counts = res[user]
    expand_buckets(buckets, bucket_counts, entry['d'], transaction_date)

    # count transaction date entries per bucket
    bucket = bisect(buckets, transaction_date) - 1
    bucket_counts[bucket][type] += 1

bisect电话使挑选合适的铲斗变得容易和快速。

您的示例输入的结果是:

>>> pprint(dict(res))
{201L: [{'D': 1, 'W': 0},
        {'D': 0, 'W': 1}],
 202L: [{'D': 0, 'W': 1},
        {'D': 0, 'W': 0},
        {'D': 1, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 1, 'W': 0}],
 203L: [{'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 1, 'W': 0}],
 204L: [{'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 1},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 0, 'W': 0},
        {'D': 1, 'W': 0}]}
于 2013-04-24T14:24:45.367 回答