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 702799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:45:39+00:00 2026-05-14T03:45:39+00:00

Trying to work with groupby so that I can group together files that were

  • 0

Trying to work with groupby so that I can group together files that were created on the same day. When I say same day in this case, I mean the dd part in mm/dd/yyyy. So if a file was created on March 1 and April 1, they should be grouped together because the “1” matches. Here’s the code I have so far:

#!/usr/bin/python
import os
import datetime
from itertools import groupby

def created_ymd(fn):
  ts = os.stat(fn).st_ctime
  dt = datetime.date.fromtimestamp(ts)
  return dt.year, dt.month, dt.day

def get_files():
  files = []
  for f in os.listdir(os.getcwd()):
    if not os.path.isfile(f): continue
    y,m,d = created_ymd(f)
    files.append((f, d))
  return files

files = get_files()
for key, group in groupby(files, lambda x: x[1]):
  for file in group:
    print "file: %s, date: %s" % (file[0], key)
  print " "

The problem is, I get lots of files that get grouped together based on the day. But then I’ll see multiple groups with the same day. Meaning I might have 4 files grouped that were created on the 17th. Later on I’ll see another unique set of 2 files that are also created on the 17th. Where am I going wrong?

  • 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-14T03:45:39+00:00Added an answer on May 14, 2026 at 3:45 am

    groupby() produces a new group every time the key changes, which means you have to sort your data first in order to group all similar elements together. Try this instead:

    files = sorted(get_files(), key=(lambda x: x[1]))

    and then run your for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to group contents that belong on a same date together for display,
I'm trying to group together values in a CASE statement in SQL Server .
I am trying to work out why I can not access the nested function
I'm trying to return a group of products in my rails so that I
I am trying to put together a query that gives me a report of
How can one get records with highest/smallest per group? Former title of this question
I am trying to work this solution on how to collapse the records based
I'm trying to work out if this is possible, let me give an example.
I have been trying to get this query to work but I am stumbed
I am trying work out with MERGE statment to Insert / Update Dimension Table

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.