untrue for anything else from 0 to 100

Jan 13, 2010 03:25

There are forty-seven letters in this sentence.

I'm too lazy to check them myself, so I wrote this... )

Leave a comment

Comments 5

gen_witt January 13 2010, 17:42:26 UTC
Neat. Some pythonisms you might not be aware of,

zip(range(len(num)), num) can be written as enumerate(num)

and

for i in [(0, 'zero'), (10, 'ten'), (11, 'eleven'), (12, 'twelve'), (13, 'thirteen'), (15, 'fifteen'), (18, 'eighteen')]: num[i[0]] = i[1]

can be written as

num.update({0: 'zero', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 15: 'fifteen', 18: 'eighteen'})

The append() call with all the ternaries doesn't feel right it python. You'd usually just see it as a if: elif: else: block.

Reply

greatbiggary January 14 2010, 01:18:58 UTC
Awesome, thanks. I had known of enumerate, but completely forgot. I actually looked around to see if there was a built-in or library for number names, just to simplify things.

Reply

greatbiggary January 14 2010, 04:16:14 UTC
I gave the update line a try, but it says:

AttributeError: 'list' object has no attribute 'update'

Is that something from a newer version than I have? I can't search for it. Every attempt through Google takes me to the use of the word 'update' for other reasons.

Reply

gen_witt January 15 2010, 02:19:06 UTC
No i'm an idiot. Update is for dictionaries.

Reply


Leave a comment

Up