Think Python by Allen B. Downey
Author:Allen B. Downey [Allen B. Downey]
Language: eng
Format: epub, mobi, pdf
Tags: COMPUTERS / Programming Languages / Python
ISBN: 9781449330712
Publisher: O'Reilly Media
Published: 2012-08-08T16:00:00+00:00
Random Words
To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:
def random_word(h): t = [] for word, freq in h.items(): t.extend([word] * freq) return random.choice(t)
The expression [word] * freq creates a list with freq copies of the string word. The extend method is similar to append except that the argument is a sequence.
Exercise 13-7.
This algorithm works, but it is not very efficient; each time you choose a random word, it rebuilds the list, which is as big as the original book. An obvious improvement is to build the list once and then make multiple selections, but the list is still big.
An alternative is:
Use keys to get a list of the words in the book.
Download
Think Python by Allen B. Downey.mobi
Think Python by Allen B. Downey.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12571)
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Dependency Injection in .NET by Mark Seemann(9340)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7087)
Microservices with Go by Alexander Shuiskov(6854)
Practical Design Patterns for Java Developers by Miroslav Wengner(6774)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6713)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6419)
Angular Projects - Third Edition by Aristeidis Bampakos(6120)
The Art of Crafting User Stories by The Art of Crafting User Stories(5649)
NetSuite for Consultants - Second Edition by Peter Ries(5582)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5386)
Kotlin in Action by Dmitry Jemerov(5066)
