[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
CHAPTER 18 Data Structures “Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Set Foo” Data structures are a central ...
Python objects can be implemented in either Python or an integrated language such as C. Types coded in C use patterns similar to ...
Operation Top is end-of-list Top is front-of-list Top is front-of-list Pop top = stack[-1]; del stack[-1] top = stack[0]; del st ...
For instance, to add logic that monitors the number of stack operations a program performs, we’d have to add code around each ha ...
This module creates a list object (stack) and exports functions to manage access to it. The stack is declared global in function ...
... print(stack1.item(i), end=' ') m a p s >>> A Stack Class Perhaps the biggest drawback of the module-based stack is ...
return len(self.stack) # len(instance), not instance def add(self, other): return Stack(self.stack + other.stack) # instance1 + ...
Like lists and dictionaries, Stack defines both methods and operators for manipulating instances by attribute references and exp ...
Example 18-3. PP4E\Dstruct\Basic\stacklog.py "customize stack for usage data" from stack2 import Stack # extends imported Stack ...
unchanged, even if the internals are. There are a variety of ways to implement stacks, some more efficient than others. So far, ...
raise IndexError() # so 'in' and 'for' stop def repr(self): return '[FastStack:' + repr(self.stack) + ']' This class’s getitem m ...
offset −1 (top is end-of-list here). Compared to using built-in lists directly, this class incurs some performance degradation f ...
([Stack:['spam']], [Stack:[3.1415, 123]]) >>> y.top() 123 Timing the Improvements The prior section’s in-place changes ...
import stack4 # in-place stacks: y.append(x) import timer # general function timer utility rept = 200 from sys import argv pushe ...
The in-place change stacks (stack4) are almost always fastest, unless no indexing is done at all—tuples (stack3) win by a hair i ...
For instance, given a set of engineers and a set of writers, you can pick out individuals who do both activities by intersecting ...
>>> x = set({'spam':[1, 1], 'ham': [2, 2], 'eggs':[3, 3]}) >>> x {'eggs', 'ham', 'spam'} Plus there are additi ...
res.append(x) return res These functions work on any type of sequence—lists strings, tuples, and other iterable objects that con ...
These multi-operand functions work on sequences in the same way as the originals, but they also support three or more operands. ...
def union(self, other): res = self.data[:] # make a copy of my list for x in other: if not x in res: res.append(x) return Set(re ...
«
66
67
68
69
70
71
72
73
74
75
»
Free download pdf