Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 987215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:26:39+00:00 2026-05-16T05:26:39+00:00

I want to split the calendar into two-week intervals starting at 2008-May-5 , or

  • 0

I want to split the calendar into two-week intervals starting at 2008-May-5, or any arbitrary starting point.

So I start with several date objects:

import datetime as DT

raw = ("2010-08-01",
       "2010-06-25",
       "2010-07-01",
       "2010-07-08")

transactions = [(DT.datetime.strptime(datestring, "%Y-%m-%d").date(),
                 "Some data here") for datestring in raw]
transactions.sort()

By manually analyzing the dates, I am quite able to figure out which dates fall within the same fortnight interval. I want to get grouping that’s similar to this one:

# Fortnight interval 1
(datetime.date(2010, 6, 25), 'Some data here')
(datetime.date(2010, 7, 1), 'Some data here')
(datetime.date(2010, 7, 8), 'Some data here')

# Fortnight interval 2
(datetime.date(2010, 8, 1), 'Some data here')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:26:40+00:00Added an answer on May 16, 2026 at 5:26 am
    import datetime as DT
    import itertools
    
    start_date=DT.date(2008,5,5)
    
    def mkdate(datestring):
        return DT.datetime.strptime(datestring, "%Y-%m-%d").date()
    
    def fortnight(date):
        return (date-start_date).days //14
    
    raw = ("2010-08-01",
           "2010-06-25",
           "2010-07-01",
           "2010-07-08")
    transactions=[(date,"Some data") for date in map(mkdate,raw)]
    transactions.sort(key=lambda (date,data):date)
    
    for key,grp in itertools.groupby(transactions,key=lambda (date,data):fortnight(date)):
        print(key,list(grp))
    

    yields

    # (55, [(datetime.date(2010, 6, 25), 'Some data')])
    # (56, [(datetime.date(2010, 7, 1), 'Some data'), (datetime.date(2010, 7, 8), 'Some data')])
    # (58, [(datetime.date(2010, 8, 1), 'Some data')])
    

    Note that 2010-6-25 is in the 55th fortnight from 2008-5-5, while 2010-7-1 is in the 56th. If you want them grouped together, simply change start_date (to something like 2008-5-16).

    PS. The key tool used above is itertools.groupby, which is explained in detail here.

    Edit: The lambdas are simply a way to make “anonymous” functions. (They are anonymous in the sense that they are not given names like functions defined by def). Anywhere you see a lambda, it is also possible to use a def to create an equivalent function. For example, you could do this:

    import operator
    transactions.sort(key=operator.itemgetter(0))
    
    def transaction_fortnight(transaction):
        date,data=transaction
        return fortnight(date)
    
    for key,grp in itertools.groupby(transactions,key=transaction_fortnight):
        print(key,list(grp))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split a file containg HTTP response into two files: one containing
I want to split a string into two parts, the string is almost free
I want to split a file(suppose a mp3) into four parts.I have tried this
I want to split a string in Javascript using split function into 2 parts.
I want to split a string like this: abc//def//ghi into a part before and
We want to split our large asp.net mvc web application into multiple Visual Studio
I want to split a string into each single character. Eg: Splitting : Geeta
I want split integers into their factors. For example, if the total number of
I want to split the qc column every 255 characters into multiple strings and
I want to split a bezier curve into a polygonal chain with n straight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.