I have a list of 760 files, from which I extract 2 lines of data, which are then stored in a dictionary:
output = {'file number':data, '1':somedatain1, '2':somedatain2, ... '760':somedatain760}
N.B.
The numbers are strings because they have been obtained by doing an os.listdir('.') in order to get a list of the filenames and splitting the string down. [I could convert this into an integer number (using int()
) if needed]
The dictionary is then printed by creating a list of the keys and iterating:
keys = output.keys()
for x in keys:
print(x, '\t', output[x])
However the output is in a random order [because of the unordered nature of a dictionary, which is, I believe, an inherent property - although I don't know why this is] and it would be far more convenient if the output was in numerical order according to the file number. This, then throws up the question:
Given that my list of keys is either
1.keys = ['filename', '2', '555', '764' ... '10']
or, if i change the string of the file number to an integer:
2.keys = ['filename', 2, 555, 764 ... 10]
how do i sort my list of keys according to the numeric value of the file number if it is strings (as shown in 1. above), or if it is of mixed object types (i.e. 1 string and 760 integers as shown in 2 above)?