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? -
-
mylist=["word_number_25_05_2011_XX", "word_number_14_04_2011_YY", "word_number_17_04_2011_XY"]
-
-
sorted(mylist, key=lambda x: x[2:5])
-
print mylist
-
-
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
3 4105 bvdet 2,851
Expert Mod 2GB
Sort on the character positions (12:) or split the string on the "_" character. - >>> mylist=["word_number_25_05_2011_XX", "word_number_14_04_2011_YY", "word_number_17_04_2011_XY"]
-
>>> mylist = sorted(mylist, key=lambda x: x[12:])
-
>>> mylist
-
['word_number_14_04_2011_YY', 'word_number_17_04_2011_XY', 'word_number_25_05_2011_XX']
-
>>> 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"]
-
>>> mylist = sorted(mylist, key=lambda x: x.split("_")[2:5])
-
>>> mylist
-
['word_number_14_04_2011_YY', 'word_number_17_03_2010_XY', 'word_number_17_04_2011_XY', 'word_number_25_05_2011_XX']
-
>>>
Note that built-in function sorted() returns the sorted list and does not sort the list in place.
But this then only sorts by the day, and does not take into account month and year?
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: - def comp(a, b, *keys):
-
if len(keys)<2:
-
return cmp(a,b)
-
a1 = a.split("_")
-
b1 = b.split("_")
-
for i in keys[:-1]:
-
x = cmp(a1[i],b1[i])
-
if x:
-
return x
-
return cmp(a1[keys[-1]], b1[keys[-1]])
-
-
mylist = ["word_number_25_05_2011_XX",
-
"word_number_14_04_2011_YY",
-
"word_number_17_04_2011_XY",
-
"word_number_17_03_2011_XY",
-
"word_number_17_03_2010_XY",
-
"word_number_17_02_2010_XY",
-
"word_number_16_02_2010_XY"]
-
-
mylist1 = sorted(mylist, lambda x,y: comp(x,y,4,3,2))
-
print
-
print mylist1
-
-
-
-
from operator import itemgetter
-
-
mylist = ["word_number_25_05_2011_XX",
-
"word_number_14_04_2011_YY",
-
"word_number_17_04_2011_XY",
-
"word_number_17_03_2011_XY",
-
"word_number_17_03_2010_XY",
-
"word_number_17_02_2010_XY",
-
"word_number_16_02_2010_XY"]
-
-
mylist=sorted([item.split("_") for item in mylist], key=itemgetter(4,3,2))
-
mylist = ["_".join(item) for item in mylist]
-
print mylist
There is probably a better way. Can someone improve on the above?
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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 =...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |