#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Group data by week and month


    Given the list below, I want a function returns lists with all dates falling in same week in one list. I'd like to do same for months.

    i.e. I'd like all dates falling into same week returned in one list and all dates falling in subsequent list returned in other lists.

    so the function will return an 'n' number of lists depending on the number of distinct week entries in the input

    Ditto for months

    Code:
    ( "2013-07-29", "2013-07-30", "2013-07-31" "2013-08-01", "2013-08-02", 
    "2013-08-05", "2013-08-06", "2013-08-07", "2013-08-08", "2013-08-09",)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    You might try first to iterate through all the entries of the list(it's actually a tuple) using a for loop.

    Code:
    for entry in your_data:
        "do something to that entry"
    after that you can do any tests or conversion to each entry. For example if I wanted to print certain dates I could try something

    Code:
     for entry in your_data:
        if entry.split('-')[i] == : #enter something
            print(entry)
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by Elbi
    Given the list below, I want a function returns lists with all dates falling in same week in one list. I'd like to do same for months.
    Just to give you a hint, here is a function that puts all dates of the same month in a list of lists (one list per month).

    (I used this code with Python 3)
    python Code:
    from datetime import datetime;
     
    def updateDateList(
                        p_dateValsList, 
                        p_resultList = [
                                        [], [], [], [], 
                                        [], [], [] ,[], 
                                        [], [], [], []
                                       ]
                      ):
        if not p_dateValsList:  
            return p_resultList
        else:
            dateVal = datetime.strptime(p_dateValsList[0] , '%Y-%m-%d').date()
            p_resultList[int(dateVal.strftime("%m")) - 1][0:0] = [dateVal]
            return updateDateList(p_dateValsList[1:], p_resultList)
     
    dateVals = ["2013-07-29", "2013-07-30", "2013-07-31", "2013-08-01", 
                "2013-08-02", "2013-08-05", "2013-08-06", "2013-08-07", 
                "2013-08-08", "2013-08-09"
               ]
     
    resultList = updateDateList(dateVals)
    print (resultList)

    And the output
    Code:
    $ python -tt myscript.py
    [[], [], [], [], [], [], [datetime.date(2013, 7, 31), datetime.date(2013, 7, 30), datetime.date(2013, 7, 29)], [datetime.date(2013, 8, 9), dateti
    me.date(2013, 8, 8), datetime.date(2013, 8, 7), datetime.date(2013, 8, 6), datetime.date(2013, 8, 5), datetime.date(2013, 8, 2), datetime.date(20
    13, 8, 1)], [], [], [], []]
    $
    I'm sure that there should be much more elegant ways of doing this (maybe by using list comprehension)
    Last edited by dariyoosh; August 20th, 2013 at 02:08 PM.
    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo