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

  • Home
  • SEARCH
  • 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 8120137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:01:50+00:00 2026-06-06T05:01:50+00:00

I am newbie in Python and I need help: I have a file with

  • 0

I am newbie in Python and I need help:

I have a file with lines like these(see below). I want to copy lines from this file and since some lines have similar date, If lines have similar date I want to choose line with smallest value of the number ending the list.

'1990-01-01','f','2'
'1990-01-02','c','4'
'1990-01-01','j','2.5'
'1990-01-01','j','1.5'
'1990-01-02','b','3.9'
  • 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-06-06T05:01:52+00:00Added an answer on June 6, 2026 at 5:01 am

    Some code first:

    text_data = """'1990-01-01','f','2'
    '1990-01-02','c','4'
    '1990-01-01','j','2.5'
    '1990-01-01','j','1.5'
    '1990-01-02','b','3.9'
    """
    
    import csv
    from StringIO import StringIO
    from operator import itemgetter
    from itertools import groupby
    
    
    temp = StringIO(text_data)
    tabular = list(csv.reader(temp, quotechar="'"))
    tabular.sort(key=lambda L: (L[0], float(L[2]))
    
    for key, val in groupby(tabular, itemgetter(0)):
        print next(val) # or do something appropriate
    

    I’ve put the text in the code and wrapped it in a StringIO object to emulate a file, but normally one would open the text using the csv.reader on a file using open().

    Then the data is sorted by the data column (index 0), and the value column (index 2) – this sort order ensures that identical dates are together (I realise you mentioned ‘similar’, but this recipe can be adapted) and that for each date, the lowest value column appears first.

    itertools.groupby() is used to iterate over keys that match certain criteria – where they meet that criteria, they are in the same “group”. ‘key’ (what is being grouped on) will be the value of the date, and ‘val’ will be another iterable of available values within that key. Since only the first is required, using next(val) will return the result with the smallest value.

    The output is:

    ['1990-01-01', 'j', '1.5']
    ['1990-01-02', 'b', '3.9']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie in Python need help! I have a file with a structure: [timestamp] [level]
I am a newbie to Python. I have a HTML file where I need
I'm newbie in Python. I have this simple code a = 0 b =
Newbie to Python, so this may seem silly. I have two dicts: default =
I am newbie to python and don't know how to do this. I have
Python newbie at time of writing. This came up because I want a user
Newbie Python question. I have a class that inherits from several classes, and some
newbie python programmer here. I have written a script and want to modify the
I'm a Python newbie. I have a series of objects that need to be
I'm newbie for python, I'm having task so I need to scan wifi and

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.