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)
collections.OrderedDict. Regular dictionaries do not guarantee the order of the keys.