#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Ordering a list of dictionaries by key


    Hi guys, I'm new to python and don't think i fully understand the data structures I am creating/using.

    I'm trying to import a file, where I create fields by the characters in each line and then put my data it into a list of dictionaries before doing some formatting and inserting my dictionaries into a mongo database.
    I have my data in the correct format, but what I want to do is sort it by the field "house_id".

    Here is my code:
    Code:
    the_file = open('myfile.txt', 'r')
    data_list = the_file.readlines()
    
    
    def bulk_data(line):
        return {'event_type': line[0:2],
                'event_sequence': line[3:7],
                'house_number': line[79:99]}
    
    
    detail_data = [bulk_data(line) for line in data_list]
    
    for item in detail_data:
        new_data = {
            'event_type': item['event_type'],
            'house_number': item['house_number'],
            'event_sequence': item['event_sequence'],
            'date_created': datetime.datetime.utcnow(),
            'more_formatting': 'event stuff'
        }
    
        print new_data
        
        #Sorting my data by house_number (the stuff I'm doing wrong)
        # for x in sorted(new_data, key=lambda k: k['house_number']):
        #     print x
    The output of my new_data is:
    Code:
    {'house_number': 'IF00001', 'more_formatting': 'event stuff', 'event_type': 'I ', 'event_sequence': '0001'}
    {'house_number': 'THA/HXGT652/030', 'more_formatting': 'event stuff', 'event_type': 'P ', 'event_sequence': '0002'}
    {'house_number': 'IF00001', 'more_formatting': 'event stuff', 'event_type': 'I ', 'event_sequence': '0003'}
    {'house_number': 'EUD/PTNN17/1800', 'more_formatting': 'event stuff', 'event_type': 'P ', 'event_sequence': '0004'}
    {'house_number': 'IF00001', 'more_formatting': 'event stuff', 'event_type': 'I ', 'event_sequence': '0005'}
    How would I be able to sort my data by "house_id" before inserting into my database? Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    You can't sort a dictionary, as dictionaries are unordered. To sort a list of dictionaries, that sorted() call you have is quite correct -- but you have to call it on the list, not on a dictionary.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Thank you for your reply. Yes I wasn't sure if dictionaries were sortable. However, I have tried to sort it before I put it into a dictionary and it failed. Could you give me an example of the implementation? Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Or you could use an ordered dictionary from the collections module.

    Comments on this post

    • Dietrich agrees : I agree
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by donfanzu23
    Thank you for your reply. Yes I wasn't sure if dictionaries were sortable. However, I have tried to sort it before I put it into a dictionary and it failed. Could you give me an example of the implementation? Thanks
    Code:
    with open('myfile.txt', 'r') as the_file:
        data_list = the_file.readlines()
    
    
    def bulk_data(line):
        return {'event_type': line[0:2],
                'event_sequence': line[3:7],
                'house_number': line[79:99]}
    
    
    detail_data = [bulk_data(line) for line in data_list]
    
    for item in detail_data:
        new_data = {
            'event_type': item['event_type'],
            'house_number': item['house_number'],
            'event_sequence': item['event_sequence'],
            'date_created': datetime.datetime.utcnow(),
            'more_formatting': 'event stuff'
        }
    
        new_data_list.append(new_data)
    
    new_data_list.sort(key=lambda d: d["house_number"])
    There. I also fixed the part where you left an open file lying around.

    Originally Posted by SuperOscar
    Or you could use an ordered dictionary from the collections module.
    There isn't an easy way to sort an OrderedDict, though. And it looks to me like he wants to sort the list, not the dictionaries in it, anyway.

    Comments on this post

    • b49P23TIvg agrees : OrderedDict dict subclass that remembers the order entries were added
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Thumbs up


    Hey, thanks a mil for all the help. I've learned alot about sorting data structures. Yeah I did indeed want to sort the list, sorry for not making that clear. Code works great Nyktos, thank you.

IMN logo majestic logo threadwatch logo seochat tools logo