[Tutor] Algorithm
Alan Gauld
alan.gauld at btinternet.com
Mon Aug 24 20:03:35 CEST 2009
"kreglet" <kreglet at gmail.com> wrote
> The reason I used print sorted is that using just print throws a syntax
> error:
>
> print (lettercount.iteritems(), key=itemgetter(1)) ---> error
> print lettercount.iteritems(), key=itemgetter(1) ---> error
> print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works
>
> I don't know why. Seems to me that any of the above should work.
Nope, print takes a string (or something that can be converted to
a string) as its argument. It does not know what key=itemgetter(1)
means. That only makes sense to a sort function.
You would need
print lettercount.iteritems()
Although I'm not sure that would actually print what you expect!
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list