0

I have a data set like this

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, 3, 19, 0, 0), u'e': datetime.datetime(2008, 8, 15, 15, 45, 10)}]

How can i bring it into the dictionary of below format

res={u'201L':(1,0,1),(2,1,0),(3,0,0),(4,0,0).. so on till (12,0,0),'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)}

where 1,2,3 is the first, second month and so on from their card issue date i.e for 201L issue date is datetime.datetime(2007, 12, 29, 0, 0), 202L it is datetime.datetime(2007, 12, 30, 0, 0) first month means from 2007-12-29 to 2008-1-29

(1,0,1)

where

1 is the first month
0 is no of times W
1 is no of times D

Edited by pyTony: fixed formating

2
Contributors
1
Reply
42
Views
3 Years
Discussion Span
Last Post by zjtpjs4
0

Do you mean you want to iterate through the original list and convert it into the given format? I don't entirely understand what you mean by 'first month' you've got a range of months there. If your data is in order you could use something like:

res = {}

for d in data:
    l = d["c"]
    if l not in res:
        res[l] = []
    number = len(res[l]) + 1
    res[l].append((number, n_of_W, n_of_D))

where number is the first number and n_of_W and n_of_D are the other numbers. I'm not sure where to get those numbers from, I see 'b' but there is one per entry. Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.