473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Ordering a list by date substring

I am trying to write some code so it orders the list by the date in the substring.

I am trying to use 'sorted', or possibly .sort

Can anyone give me some pointers?

Expand|Select|Wrap|Line Numbers
  1.  
  2. mylist=["word_number_25_05_2011_XX", "word_number_14_04_2011_YY", "word_number_17_04_2011_XY"]
  3.  
  4. sorted(mylist, key=lambda x: x[2:5])
  5. print mylist
  6.  
  7.  
I'm trying to get it so it will print:

word_number_14_04_2011_YY
word_number_17_04_2011_XY
word_number_25_05_2011_XX
Jun 15 '12 #1
3 4105
bvdet
2,851 Expert Mod 2GB
Sort on the character positions (12:) or split the string on the "_" character.
Expand|Select|Wrap|Line Numbers
  1. >>> mylist=["word_number_25_05_2011_XX", "word_number_14_04_2011_YY", "word_number_17_04_2011_XY"]
  2. >>> mylist = sorted(mylist, key=lambda x: x[12:])
  3. >>> mylist
  4. ['word_number_14_04_2011_YY', 'word_number_17_04_2011_XY', 'word_number_25_05_2011_XX']
  5. >>> mylist=["word_number_25_05_2011_XX", "word_number_14_04_2011_YY", "word_number_17_04_2011_XY", "word_number_17_03_2010_XY"]
  6. >>> mylist = sorted(mylist, key=lambda x: x.split("_")[2:5])
  7. >>> mylist
  8. ['word_number_14_04_2011_YY', 'word_number_17_03_2010_XY', 'word_number_17_04_2011_XY', 'word_number_25_05_2011_XX']
  9. >>> 
Note that built-in function sorted() returns the sorted list and does not sort the list in place.
Jun 15 '12 #2
But this then only sorts by the day, and does not take into account month and year?
Jun 15 '12 #3
bvdet
2,851 Expert Mod 2GB
You may want to sort on year, month and day in that order. Following are two ways of accomplishing that:
Expand|Select|Wrap|Line Numbers
  1. def comp(a, b, *keys):
  2.     if len(keys)<2:
  3.         return cmp(a,b)
  4.     a1 = a.split("_")
  5.     b1 = b.split("_")
  6.     for i in keys[:-1]:
  7.         x = cmp(a1[i],b1[i])
  8.         if x:
  9.             return x
  10.     return cmp(a1[keys[-1]], b1[keys[-1]])
  11.  
  12. mylist = ["word_number_25_05_2011_XX",
  13.           "word_number_14_04_2011_YY",
  14.           "word_number_17_04_2011_XY",
  15.           "word_number_17_03_2011_XY",
  16.           "word_number_17_03_2010_XY",
  17.           "word_number_17_02_2010_XY",
  18.           "word_number_16_02_2010_XY"]
  19.  
  20. mylist1 = sorted(mylist, lambda x,y: comp(x,y,4,3,2))
  21. print
  22. print mylist1
  23.  
  24.  
  25.  
  26. from operator import itemgetter
  27.  
  28. mylist = ["word_number_25_05_2011_XX",
  29.           "word_number_14_04_2011_YY",
  30.           "word_number_17_04_2011_XY",
  31.           "word_number_17_03_2011_XY",
  32.           "word_number_17_03_2010_XY",
  33.           "word_number_17_02_2010_XY",
  34.           "word_number_16_02_2010_XY"]
  35.  
  36. mylist=sorted([item.split("_") for item in mylist], key=itemgetter(4,3,2))
  37. mylist = ["_".join(item) for item in mylist]
  38. print mylist
There is probably a better way. Can someone improve on the above?
Jun 15 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: TomT | last post by:
I have created a DB in Access 2000. The Date Field (clientdate) is currently a MEMO field. The date is being entered as: 10 June 2004 21 January 2005 09 September 2004
2
by: Lauren Quantrell | last post by:
I have a table with a startdatetime and an enddatetime column such as: StartDateTime EndDateTime what I want to see returned is: 01/29/2004 10:30AM 01/29/2004 1:30PM "1/29/2004...
11
by: Laszlo Zsolt Nagy | last post by:
Hello, Do you know how to implement a really efficient self reordering list in Python? (List with a maximum length. When an item is processed, it becomes the first element in the list.) I would...
3
by: Fabrizio | last post by:
HI, I'm trying to create an aspx page that lists all files that an user can download from a site simply clicking on the link. There is any example on the web? Thanks, Fabrizio
1
by: timmso | last post by:
I am trying to build a simple asp.net project. What sort of control do I need to use to simply display a list of links in a table format? For example, let's say I have a database table: tblNames...
5
by: Ray | last post by:
I have a table with some audit date and time columns. Problem is the developer who stored the data left them as DECIMAL type instead of DATE and TIME. Is there a way I can convert the DECIMAL type...
2
by: greenflame | last post by:
I am trying to reorder elements of a list and I am stuck as to what might be the best way to approach this. I have a (main) list of elements and another (ordering) list (which is may shorter, but...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
9
by: Bob | last post by:
Hi, I need to convert a string into DateTime. I can use Convert.ToDateTime() however the string can be in different formats like 19700504 or 05041970. Any ideas? Thanks
0
by: oncorhynchus | last post by:
All: CAVEAT: I'm not really an AS400 jockey, I just play one on TV. Keeping that in mind...... I'm using a legacy AS400 application where all dates are stored as Integers, e.g. 05/01/2007 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.