0

When I run this code the dictionary that is produced does not match the dates in the strings that I am extracting the dates from. How do I put this dictionary in order by date? past to future. Also why is the dictionary out of order?

page = str('<OPTION VALUE="08/25/2013" >08/25/2013</OPTION><OPTION VALUE="09/01/2013">09/01/2013</OPTION><OPTION VALUE="09/08/2013" >09/08/2013</OPTION><OPTION VALUE="09/15/2013" >09/15/2013</OPTION><OPTION VALUE="09/22/2013" >09/22/2013</OPTION><OPTION VALUE="09/29/2013" >09/29/2013</OPTION><OPTION VALUE="10/06/2013" >10/06/2013</OPTION><OPTION VALUE="10/13/2013" >10/13/2013</OPTION><OPTION VALUE="10/20/2013">10/20/2013</OPTION><OPTIONVALUE="10/27/2013">10/27/2013</OPTION><OPTION VALUE="11/03/2013" >11/03/2013</OPTION><OPTION VALUE="11/10/2013" >11/10/2013</OPTION><OPTION VALUE="11/17/2013" >11/17/2013</OPTION><OPTION VALUE="11/24/2013" >11/24/2013</OPTION><OPTION VALUE="12/01/2013" >12/01/2013</OPTION><OPTION VALUE="12/08/2013" >12/08/2013</OPTION><OPTION VALUE="12/15/2013" >12/15/2013</OPTION>OPTION VALUE="12/22/2013" >12/22/2013</OPTION><OPTION VALUE="12/29/2013" >12/29/2013</OPTION><OPTION VALUE="01/05/2014" >01/05/2014</OPTION><OPTION VALUE="01/12/2014" >01/12/2014</OPTION><OPTION VALUE="01/19/2014" >01/19/2014</OPTION><OPTION VALUE="01/26/2014" >01/26/2014</OPTION><OPTION VALUE="02/02/2014" >02/02/2014</OPTION><OPTION VALUE="02/09/2014" >02/09/2014</OPTION><OPTION VALUE="02/16/2014" >02/16/2014</OPTION><OPTION VALUE="02/23/2014" >02/23/2014</OPTION><OPTION VALUE="03/02/2014" >03/02/2014</OPTION><OPTION VALUE="03/09/2014" >03/09/2014</OPTION><OPTION VALUE="03/16/2014" >03/16/2014</OPTION><OPTION VALUE="03/23/2014" >03/23/2014</OPTION><OPTION VALUE="03/30/2014" >03/30/2014</OPTION><OPTION VALUE="04/06/2014" >04/06/2014</OPTION><OPTION VALUE="04/13/2014" >04/13/2014</OPTION><OPTION VALUE="04/20/2014" >04/20/2014</OPTION><OPTION VALUE="04/27/2014" >04/27/2014</OPTION><OPTION VALUE="05/04/2014" >05/04/2014</OPTION><OPTION VALUE="05/11/2014" >05/11/2014</OPTION><OPTION VALUE="05/18/2014" >05/18/2014</OPTION><OPTION VALUE="05/25/2014" >05/25/2014</OPTION><OPTION VALUE="06/01/2014" >06/01/2014</OPTION><OPTION VALUE="06/08/2014" >06/08/2014</OPTION><OPTION VALUE="06/15/2014" >06/15/2014</OPTION>')


def web_link (enter_web_link):
#11%2F10%2F2013
    enter_web_link =  enter_web_link.replace("/","%") #00%00%0000
    add_twoF = enter_web_link[:3]+"2F"+ enter_web_link[3:]  #00%2F00%0000
    add_twoF_everywhere = add_twoF[:8] +"2F"+add_twoF[8:]
    add_twoF_everywhere = str(add_twoF_everywhere)
    return  add_twoF_everywhere

def search_13(page):
    starter = '<OPTION VALUE="' # find the postion where this starts
    start_link = page.find(starter)
    starter = len(starter)
    if start_link == -1:
        return None, 0 
    start_link = start_link + starter
    end_date = start_link + 10
    datetext  = page[start_link: end_date]
    str_date = str(datetext) #this is hte actuall normal looking date dd/mm/yyyy
    enter_web_link = str_date 
    endoflinkdate = web_link(enter_web_link)
    return str_date , end_date,  endoflinkdate


def getalllinks(page):
    links = {}  
    while True:
        str_date,end_date,endoflinkdate = search_13(page)
        if str_date:
            links[str_date] ='dont want you to have this link' +  endoflinkdate
            page = page[end_date:]
        else:
            break
    return links



work = getalllinks(page)
print work

I heard the sort function only works for lists and also each date is a string. I would rather have a way that does not use a library.

when I try using:

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first encountered'

     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

it i get an eror Counter is not defined. How am I supposed to use this?

user2330624
  • 303
  • 1
  • 7
  • 16
  • 4
    Dictionaries are not a ordered type. Use an [`OrderedDict`](http://docs.python.org/2/library/collections.html#collections.OrderedDict) if you really want ordering. – Amber Dec 01 '13 at 20:00
  • 2
    The documentation is clear on the subject. You should make sure you read it. – David Heffernan Dec 01 '13 at 20:04

1 Answers1

3

Dictionaries are by definition unordered. If you care about the (insertion) order, you can use collections.OrderedDict. Otherwise you could iterate over the keys in order and then access the elements of the dict:

for key in sorted(d.keys()):
    print(d[key])
poke
  • 369,085
  • 72
  • 557
  • 602