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]

Define a score function

def score(item):
    return int(item[0])
z = {'11': [0, 1], '3': [1, -2, 3], '4':[2, 3], '2':[3, -4, 5], '0':[4]}
sorted_z = sorted(z.iteritems(), key=score)
print "z=", sorted_z

for i in range(len(sorted_z)):
     print sorted_z[i][0], " ".join(str(x) for x in sorted_z[i][1])

Maybe this helps:

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(((int(x),values) for x, values in z.iteritems()), key=operator.itemgetter(0))
print "z=", ''.join('%s %s\n' % pair for pair in sorted_z)

Many thanks for the quick reply! Score function works like a charm. Can you explain a little bit more?

I can see "sorted(((int(x),values) for x, values in z.iteritems()), key=operator.itemgetter(0))" works, but how come this is not, for key in z: int(key) # and there is no error message

I have a problem in output fromat and want to know how to fix it,
print "%s\t %.4f\t %.4f\t %.4f\t...." % (a, b, c, d,...)

if positive and negative float number in the same column, it won't delimited by a Tab, only when the sign is same, the line up will be perfect. Where am I wrong?

Thanks again!

You mean something like this with (fixed minumum) width for every width.

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(((int(x),values) for x, values in z.iteritems()), key=operator.itemgetter(0))

print '\n'.join(("%4s = " % key+len(values)*"%10.4f")
                % tuple(values)
                for key, values in sorted_z)
for key in z: int(key)

This code does nothin as result of int(key) is not used anywhere similar to for example:

for nothing in range(10):'nothing'

which does nothing in script file, but outputs the 'nothing' 10 times if entered in command line.

Same way the above your code from idle prompt after running above code:

>>> 
   0 =     4.0000
   2 =     3.0000   -4.0000    5.0000
   3 =     1.0000   -2.0000    3.0000
   4 =     2.0000    3.0000
  11 =     0.0000    1.0000
>>> for key in z: int(key)

11
0
3
2
4
>>>

Hi Tony,
I see, python seems right handed if you fix the length, right? And thanks for the clarifications, so the key is still in type string after sorted in the dictionary. Great to know this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.