Ordering a list by date substring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pystarter
    New Member
    • May 2012
    • 6

    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?

    Code:
    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Sort on the character positions (12:) or split the string on the "_" character.
    Code:
    >>> 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.

    Comment

    • pystarter
      New Member
      • May 2012
      • 6

      #3
      But this then only sorts by the day, and does not take into account month and year?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You may want to sort on year, month and day in that order. Following are two ways of accomplishing that:
        Code:
        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?

        Comment

        Working...