1 #Encoding:utf-82 #3 """@version: 1.04 @author Heng5 @time: 6/24/2016 4:52 PM6 7 """8 9 " "Ten collections is a python built-in collection module One A Learn some of the common functions - " " - #Namedtuple used to create a custom tuple object the - fromCollectionsImportNamedtuple - - #define a 2-D coordinate +Point = Namedtuple (' Point', ['x','y']) -p = Point (1, 3) + Printp.x, P.y A #result 1 3 at #Define a circle - #namedtuple (' name ', [Property list]): -Circle = Namedtuple ('Circle', ['x','y','R']) -c = Circle (1, 1, 3) - Printc.x, c.y, C.R - #result 1 1 3 in - to #Deque improves the INSERT and delete operations list for queues and stacks + - fromCollectionsImportdeque the *Q = deque (range (10)) $Q.append ('x')Panax NotoginsengQ.appendleft ('X1') - PrintQ the #result Deque ([' X1 ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' X ']) + A the #defaultdict, like Dict, returns a default value when resolving a key that does not exist + - fromCollectionsImportdefaultdict $ $DD = Defaultdict (Lambda:'Key not found') - Printdd['Key'] - the #result key not found - Wuyi #Counter a counter the - fromCollectionsImportCounter Wu -c =Counter () About forCharinch "My name is Niu,what ' s your name": $C[char] = C[char] + 1 - PrintC - - #result counts the number of occurrences of a character A #Counter ({': 5, ' a ': 3, ' m ': 3, ' n ': 3, ' E ': 2, ' I ': 2, ' s ': 2, ' U ': 2, ' Y ': 2, ' ' ': 1, ' H ': 1, ', ': 1, ' O ': 1, ' R ': 1, ' t ': 1, ' W ': 1}) + the #ordereddict an orderly dict function - $ fromCollectionsImportordereddict the theD = {'a': 1,'b': 2,'C': 3,'D': 4,"': 5} the PrintD#result{' A ': 1, ' C ': 3, ' B ': 2, ' E ': 5, ' d ': 4} Default dict is unordered collection the -b =Ordereddict (d) in Printb the the #result {' A ': 1, ' ': 5, ' C ': 3, ' B ': 2, ' d ': 4} About #ordereddict ([' A ', 1), (' ', 5 '), (' C ', 3), (' B ', 2), (' d ', 4)])
Day1_collections Library Learning