Petros Kyriakou
Petros Kyriakou

Reputation: 5343

How to sort list of dictionaries by value

dictionary[pattern_key] = {"key": index_key, "document": index_source, "startPos":index_start, "endPos": index_end}

This is an extract of my list of dictionaries

{
'AGACAATCTC': {'startPos': '174', 'document': 'source-document01012.txt', 'endPos': '183', 'key': 'AGACAATCTC'}, 
'GGTCAGACAA': {'startPos': '18', 'document': 'source-document01012.txt', 'endPos': '27', 'key': 'GGTCAGACAA'}, 
'TAGATGAAGT': {'startPos': '102', 'document': 'source-document01012.txt', 'endPos': '111', 'key': 'TAGATGAAGT'}
}

How can i sort that by document and then by startPos ?

i tried something like this but does not work

sorted_dict = sorted(dictionary, key=itemgetter(pattern_key[document]))

script.py

#!/usr/bin/env python
import sys

dictionary = {};

for pattern in sys.stdin:

    if "," in pattern:
        pattern_key, pattern_source, pattern_start, pattern_end = pattern.strip().split(",")
        index_file =  open('index.txt', 'r')

        for line in index_file:
            if "," in line:
                index_key, index_source, index_start, index_end = line.strip().split(",")
                if pattern_key == index_key:
                    dictionary[pattern_key] = {"document": index_source, "startPos":index_start, "endPos": index_end}

sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))

for k, v in dictionary.items():
    print (k, '-->', v)

Upvotes: 0

Views: 366

Answers (2)

Stewart Smith
Stewart Smith

Reputation: 1416

You can get the entries in the inner dictionary as keys for sorted:

sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))

A tuple key will be sorted first by the 0th element, then 1st, and so on.

Note that this produces a list of tuples, where each tuple is (str, dict).

EDIT:
In your context, the correct implementation is the following:

sorted_values = sorted(dictionary.items(), key = lambda x: (x[1]['document'], int(x[1]['startPos'])))

for k, v in sorted_values:
    print (k, '-->', v)

Upvotes: 3

Iron Fist
Iron Fist

Reputation: 10961

Make your sorting based on your desired criteria then create a new OrderedDict from the sorted list, since dict cannot keep the sorting by it's nature:

>>> from collections import OrderedDict
>>>
>>> d = {'AGACAATCTC': {'endPos': '183', 'document': 'source-document01010.txt', 'key': 'AGACAATCTC', 'startPos': '174'}, 'GGTCAGACAA': {'endPos': '27', 'document': 'source-document01010.txt', 'key': 'GGTCAGACAA', 'startPos': '18'}, 'TAGATGAAGT': {'endPos': '111', 'document': 'source-document01011.txt', 'key': 'TAGATGAAGT', 'startPos': '102'}}
>>> 
>>> d_ordered = OrderedDict(sorted(d.items(), key=lambda t:(t[1]['document'], int(t[1]['startPos']))))
>>> 
>>> d_ordered
OrderedDict([('GGTCAGACAA', {'endPos': '27', 'document': 'source-document01010.txt', 'key': 'GGTCAGACAA', 'startPos': '18'}), ('AGACAATCTC', {'endPos': '183', 'document': 'source-document01010.txt', 'key': 'AGACAATCTC', 'startPos': '174'}), ('TAGATGAAGT', {'endPos': '111', 'document': 'source-document01011.txt', 'key': 'TAGATGAAGT', 'startPos': '102'})])

Upvotes: 2

Related Questions