I have a dictionary with an array as elements. Say:
masterListShort = {'a': [5, 2, 1, 2], 'b': [7, 2, 4, 1], 'c': [2, 0, 1, 1]}
I would like to reverse sort this dictionary by the first element of the values. I would then like to write my output to a tab delimited file like this:
<key> <value1> <value2> <value3> etc.
My current code where I write my dictionary to file looks like this:
# write the masterListShort to file
outFile2 = open('../masterListShort.tsv', 'w')
for item in sorted(masterListShort):
tempStr = '\t'.join(map(str, masterListShort[item]))
outFile2.write(str(item) + '\t' + tempStr + '\n')
outFile2.close()
This code works fine, it just does not sort the list. I want my output to be written in a tab delimited file format. So:
b 7 2 4 1
c 5 2 1 2
a 2 0 1 1
I have found the following commands so far, and was wondering if i could apply them to my code:
import operator
sorted(myDict, key=operator.itemgetter(1))