Changing variable to integer

vertigo spam at spam.pl
Sun Dec 17 12:17:06 EST 2006


On Sun, 17 Dec 2006 17:00:46 +0100, Juho Schultz <juho.schultz at pp.inet.fi>  
wrote:

> vertigo wrote:
>> > Perhaps you meant something more along the lines of this:
>> >
>> >>>> def PrintWordCountFloat(words):
>> > 	number = 0
>> > 	for index, word in enumerate(words):
>> > 		print "%s %f" % (index, word)
>> > 		number = number + 1
>> > 	print "Total words: %d" %(number)
>> >>>> PrintWordCountFloat(range(10))
>> > 0 0.000000
>> > 1 1.000000
>> > 2 2.000000
>> > 3 3.000000
>> > 4 4.000000
>> > 5 5.000000
>> > 6 6.000000
>> > 7 7.000000
>> > 8 8.000000
>> > 9 9.000000
>> > Total words: 10
>> >
>> > Or similar; I can't read your mind. Just know that enumerate(iterable)
>> > yields (index, value) for each item in iterable.
>>
>>
>> sorry, i was not precise. words is a dictionary.
>> 1. How can i show it's all variable (with key and value) ?
>> 2. How can i show sorted dictionary (by key) ?
>> 3. Is there any function which could do fast iteration on elements of
>> sorted dictionary ?
>>
>> Thanx
>
> I hope this helps a bit:
>
>>>> words = {"help":20, "copyright":25, "credits":35}
> # show dictionary
>>>> for w, s in words.iteritems(): print w, s
> ...
> credits 35
> help 20
> copyright 25
> # show sorted dictionary
> # dicts are not ordered, so you have to sort them.
>>>> for w, s in sorted(words.iteritems()): print w, s
> ...
> copyright 25
> credits 35
> help 20
>

Thanx, it's working :)



More information about the Python-list mailing list