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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:38:47+00:00 2026-05-27T09:38:47+00:00

I have a python question, but i think any programer would know enough to

  • 0

I have a python question, but i think any programer would know enough to send me in the right direction.I have found some really good code for sorting data. Here is an example of one i want to use:

from operator import itemgetter, attrgetter

data = [('red', 1,80), ('blue', 1,900), ('red', 2,50), ('blue', 2,300)]

ss=sorted(data, key=itemgetter(0))

print ss

my problem is getting data in this specific format as above because i read my data in from a csv file and i can not type it in like the data is above, because i have data stored on files.
I read my data in to python like this:

reader = csv.reader(open("Meterdata2.csv","rb"));

name=[]; idc=[]; startD=[]; endD=[]; cons=[]; monthdays=[];

for row in reader:

if row[0] != 'IB_id':

name.append(str(row[1])) # name

idc.append(long(row[2])) # idc_acct

startD.append(tcnvrt.str2datetime(row[3]))

endD.append(tcnvrt.str2datetime(row[4]))

cons.append(int(row[6])) # consumption

monthdays.append((tcnvrt.str2datetime(row[4])-tcnvrt.str2datetime(row[3])).days)

IDC=array(idc); Name=array(name); STARTD=array(startD); # these are numpy.ndarray

END_D=array(endD); CONS=array(cons); MONTHDAYS=array(monthdays)

AA=[IDC,Name,STARTD,END_D,MONTHDAYS,CONS]

I want to sort my data by my endD variable wich is an array of dates.

I did find some functions online that i think might do the trick of getting data in the specific form to sort it,but i have not been able to figure it out. I keep writing up these function to apply to my example but i can not get them to work.

here are the function i think might work for this (if i change the variables):

   def __init__(self, name, grade, age):
            self.name = name
            self.grade = grade
            self.age = age
    def __repr__(self):
            return repr((self.name, self.grade, self.age))

can someone please push me in the right direction. Thank you

  • 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-27T09:38:48+00:00Added an answer on May 27, 2026 at 9:38 am

    I think you just want to convert it into a list of tuples, before sorting, right? You can do this by using the zip function:

    l = zip(name, idc, startD, ...)
    l.sort(key=...)
    

    but I do not understand why you do not sort the rows as they are straight away…

    l = []
    for name, idc, ... in reader:
        if name != ...:
            continue
        l.append((str(name), int(idc), ..))
    
    sorted(l, key=itemgetter(0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A question that seems to have quite a few options for Python, but none
I have a question about python mechanize's proxy support. I'm making some web client
I'm currently developing some things in Python and I have a question about variables
I know there have been similar questions posted but I think the issue I'm
I do NOT have python/ruby enabled. My Question: I frequently have to write things
I am a beginner of python and have a question, very confusing for me.
I have a question about using os.execvp in Python. I have the following bit
I have a question related to understanding of how python dictionaries work. I remember
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I've read over this question and I think that is a great start, but

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.