I have a dictionary used a string (read from file by number) as key and a list as its value (each may have different number of elements). I want to print like this:
0 4
2 3 -4 5
3 1 -2 3
4 2 3
11 0 1
When I sort on the keys, but since it is string, so '11' comes before '2',and the print like this
0 [4]
11 [0, 1]
2 [3, -4, 5]
3 [1, -2, 3]
4 [2, 3]
I think it is because of the key is string not a int, but I am not sure how to correct it and the list format. Thanks a lot for your suggestions!
import operator
z = {'11': [0, 1], '3': [1, -2, 3], '4':[2, 3], '2':[3, -4, 5], '0':[4]}
for key in z:
int(key)
sorted_z = sorted(z.iteritems(), key=operator.itemgetter(0))
print "z=", sorted_z
for i in range(len(sorted_z)):
print sorted_z[i][0], sorted_z[i][1]