随笔分类 - Python Learning
摘要:We have used many of Python’s built-in types; now we are going to define a new type. As an example, we will create a type called Point that represents...
阅读全文
摘要:Pickle translates almost any type of object into a string. pickle.dumps takes an object as a parameter and returns a string representation. ...
阅读全文
摘要:Files are organized into directories (also called ‘folders’). Every running program has a ‘current directory’, which is the default directory for most...
阅读全文
摘要:The argument of write has to be a string, so if we want to put other values in a file, we have to convert them to strings. The easiest way to do that ...
阅读全文
摘要:A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM. To read a file, you can use open to c...
阅读全文
摘要:Most of the programs we have seen so far are transient in the sense that they run for a short time and produce some output, but when they end, their d...
阅读全文
摘要: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 fre...
阅读全文
摘要:Finding the words from the book that are not in the word list from words.txt is a problem you might recognize as set subtraction; that is, we want to ...
阅读全文
摘要:We have seen built-in functions that take a variable number of arguments. For example range can take one, two or three arguments.It is possible to wri...
阅读全文
摘要:To find the most common words, we can apply the DSU pattern; most_common takes a histogram and returns a list of word-frequency tuples, sorted in reve...
阅读全文
摘要:Here is a program that reads a file and builds a histogram of the words in the file: process_file loops through the lines of the file, passi...
阅读全文
摘要:Most computer programs do the same thing every time they execute, given the same inputs, so they are said to be deterministic. Deterministic is usuall...
阅读全文
摘要:Write a program that reads a file, breaks each line into words, scripts whitespace and punctuation from the words, and converts them to lowercase. Mod...
阅读全文
摘要:DSU stands for ‘decorate, sort, undecorate’ and refers to a pattern that is often useful for sorting lists according to some attribute of elements.For...
阅读全文
摘要:I have focused on lists of tuples, but almost all the examples in this chapter also work with lists of lists, tuples of tuples, and tuples of lists. T...
阅读全文
摘要:Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair. As you should expect from a dictionary, t...
阅读全文
摘要:zip is a built-in function that takes two or more sequence and ‘zips’ them into a list of tuples, where each tuple contains one element from each sequ...
阅读全文
摘要:Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is the same as returning multiple values. For example...
阅读全文
摘要:It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary variable. This solution is cumberso...
阅读全文
摘要:A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The imp...
阅读全文

浙公网安备 33010602011771号