5 ms·
numbers = range(10) is mucho easier than typing the numbers :) words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
by mrtron 19y ago
numbers = range(10) is mucho easier than typing the numbers :)
words = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]
number_to_word = dict(zip(range(10), words))
word_to_number = dict(zip(words, range(10)))
The last approach with lots of testing is good.
- BrandonM 19y agoWell, here's the approach that was settled on in the article: import string words = "zero one two three four five six seven eight nine".split() number_to_word = dict(zip(string.digits, words)) word_to_number = dict(zip(words, range(10))) I'm not sure why string.digits was used in the first zip but not the second. As kirubakaran pointed out, though, the number_to_word dictionary isn't even needed. The best approach, in my opinion, becomes: number_to_word = "zero one two three four five six seven eight nine".split() word_to_number = dict(zip(number_to_word, range(10)) This would serve to display several cool Python idioms: the simplicity of string splitting, the power of zip, the simplicity of producing a list of numbers, and the use of the accessor on different types of data structures. If memory recalls, none of those operations are that easy to do in Java.
- dood 19y agoIt can be made a little neater, show off a little more of Python and be more versatile by replacing range(10) with count() from the iterator module [http://docs.python.org/lib/itertools-functions.html http://docs.python.org/lib/itertools-functions.html] from itertools import count words = "zero one two three four five six seven eight nine".split() word_to_number = dict(zip(words, count())) [http://codepad.org/naCDzno7 http://codepad.org/naCDzno7]
- BrandonM 19y agoI like itertools as much as the next programmer (although a large subset of it will be subsumed when Python3k switches to iterators on operations like map and zip), but I feel like using an imported module to (apparently) produce a list of numbers might actually give the wrong idea about Python. In several other languages, you can't even write to standard output without importing a module or five, whereas in Python, you can get a lot done without even touching the standard library (although you will likely want to rather quickly). Seeing an import statement at the top might give the Python newbie the idea that core Python doesn't have "basic" functionality. I think a good later lesson would be to explain how iterators and the standard library works and then revisit this code to make it look like your example, but in the beginning, I think range(10) would be simpler and would better serve the purpose of the lesson. Even in this case, xrange (at least in Python 2.x) is obviously the better choice, but introducing that complexity at this point doesn't gain much, in my opinion.
- eru 19y agoWon't range behave like xrange soon anyway?
- jacobolus 19y agoOr just: words = "zero one two three four five six seven eight nine ten".split() word_to_number = dict((j,i) for i,j in enumerate(words))