binary tuple (tuple): immutable sequence
Like List, is a sequence, the only difference is that the tuple element cannot be modified, usually with (,,) represents a tuple, or it can be without parentheses.
1 #创建元组 2 >>> 3 (1, 2, 3) 4 >>> t = () 5 >>> T 6 (1, 2, 3) 7 #创建空元组 8 >>> t 1 = () 9 >>> t110 () One #创建只有一个元素的元组, note that it is necessary to take a comma with the >>> t2 = (1,) >>> t214 (1,) #也可以用tuple () function to create tuples >>> B = tuple (' abc ') of >>> B18 (' A ', ' B ', ' C ')
"Access": like List, supports indexing and shard access.
"Difference": What is the difference between a tuple and a list and where does it apply?
1, tuples can act as keys in a dictionary, and lists are not
2, tuples do not have built-in functions like list.
Three, set (set)
Definition: The collection is an unordered set of distinct values
1 >>> basket = {' Apple ', ' orange ', ' apple ', ' pear ', ' orange ', ' banana '} 2 >>> print (basket) # show that duplicates has been removed 3 {' Orange ', ' banana ', ' pear ', ' apple '} 4 >>> ' orange ' in bask ET # Fast Membership Testing 5 True 6 >>> ' crabgrass ' in basket 7 False 8 9 >>> # Demon Strate set operations on the unique letters from the WORDS10 ... >>> a = set (' Abracadabra ')->>> B = Set (' Alacazam ')->>> A # unique letters in A14 {' A ', ' R ', ' B ', ' C ', ' d '}15 >>> A-B # letters in a N OT in B16 {' R ', ' d ', ' B '}17 >>> a | B # Letters in either a or B18 {' A ', ' C ', ' R ', ' d ', ' B ', ' m ', ' Z ', ' l '}19 >>> a & ; B # Letters in both A and B20 {' A ', ' C '}21 >>> a ^ B # Letters in a or B and not both22 {' R ',' d ', ' B ', ' m ', ' Z ', ' l '}
Iv. Dictionary (dictionaries)
A definition dictionary is a set of key-value pairs, where key can be a number, a string, and a tuple.
"Create" is denoted by curly braces, the key-value structure, separated by a colon between key values, and separated by commas between different key-value pairs, as follows:
1 >>> dict = {' Tome ': ' 1341 ', ' Helen ': ' 2030 '}2 >>> dict3 {' Tome ': ' 1341 ', ' Helen ': ' 2030 '}4 >>> D Ict1 = {}5 >>> dict16 {}
"Basic Operations"
Len (dict) returns the number of dictionary key-value pairs
DICT[K] Returns the value of key K
DICT[K] Change the value of key to K
Del Dict[k] Delete item with key K
K in Dict Check if the dict contains key K
1 #!/usr/python 2 3 people = { 4 ' Alice ': { 5 ' phone ': ' 2341 ', 6 ' addr ': ' Foo drive 23 ' 7 }, 8 9 ' Beth ': {ten ' phone ': ' 2003 ', one ' addr ': ' Bar street + ' , 13 ' Cecil ': {The ' phone ': ' 3212 ', ' addr ': ' Baz Avenue ' + } + labels = {' phone ': ' Phone number ', ' addr ': ' Address ', ' + ' = ' raw_input ' (' Name: ') request = Raw_i Nput (' Phone number (p) or address (a)? ' If request = = ' P ': key = ' phone ' if request = = ' A ': + = ' addr ' if name in people:34 print '%s '%s is%s. "% (Name,labels[key],people[name][key])
Operation Result:
1 [[email protected] python]# python dict.py 2 name:alice3 Phone number (p) or address (a)? P4 Alice ' s phone number is 2341.
Method
1, Clear () method: Empty The dictionary all items, in-situ operation.
1 >>> x = {} 2 >>> y = x #并不是简单的赋值, this can be understood as X, y together pointing to a dictionary 3 >>> x[' key ']= ' value ' 4 >>> x 5 {' key ': ' Value '} 6 >>> y 7 {' key ': ' Value '} 8 >>> x = {} 9 >>> x10 {}11 >>> y12 {' Key ': ' Value '}
1 >>> x = {} 2 >>> y = x 3 >>> x[' key ' = ' value ' 4 >>> x 5 {' key ': ' Value '} 6 >> ;> y 7 {' key ': ' Value '} 8 >>> x.clear () #注意clear方法清除原地 9 >>> x10 {}11 >>> y12 {}
2. Copy () Method: Returns a new dictionary of the same worth, shallow copy
1 >>> a = {' username ': ' admin ', ' machines ': [' foo ', ' bar ']}2 >>> b = a.copy () 3 >>> b[' username ']= ' Lsls ' 4 >>> b[' machines '].remove (' Bar ') 5 >>> A
The original dictionary can be left unchanged by deep copying:
1 >>> from copy import deepcopy2 >>> d = {}3 >>> d[' name '] = [' AA ', ' BB ']4 >>> c = d.co PY () 5 >>> DC = deepcopy (d) 6 >>> dc[' name '].append (' cc ') 7 >>> D8 {' name ': [' AA ', ' BB ']}
3, Get (Key) method: access the dictionary value through key, if the dictionary does not exist key, will not error, return none
4. Keys () and Iterkeys () method:The Keys method returns the keys in the dictionary as a list, while Iterkeys returns an iterator for the key
5, Pop (key) method: Delete the key value item of the specified key
6, Popitem () method: Randomly delete a key value entry
7. VALUES () and Itervalues () methods:The Values method returns the keys in the dictionary as a list, while Itervalues returns an iterator for the key
8. Update (item) method: updating another dictionary with one dictionary item
1 >>> d = {' title ': ' Python study ', ' url ': ' www.python.com '}2 >>> x = {' title ': '. NET study '} 3 >>> D.update (x) 4 >>> d5 {' url ': ' www.python.com ', ' title ': '. NET study '}
At this point, the data structures commonly used in Python are almost as learned.
Chapter II Important data structures in Python (ii.)