Pick Up: http://www.cnblogs.com/liu-wang/p/8973273.html
3 tuples
4 Introduction of the Dictionary 4.1 dictionary
<2> Dictionaries in software development
Variable info is a dictionary type:
info = {‘name‘:‘班长‘, ‘id‘:100, ‘sex‘:‘f‘, ‘address‘:‘地球亚洲中国北京‘}
Description
- Dictionaries and lists are also capable of storing multiple data
- When an element is found in the list, it is based on the subscript
- When looking for an element in a dictionary, it is based on the ' name ' (that is, the colon: the preceding value, such as ' name ', ' id ', ' sex ') in the code above
- Each element of the dictionary consists of 2 parts, the key: value. For example ' name ': ' Monitor ', ' name ' is the key, ' monitor ' is the value
<3> Access Values by key
info = {' name ': ' Monitor ', ' ID ': +, ' sex ': ' f ', ' address ': ' Earth Asia China Beijing '} print (info[' name ']) print (info[' Address '])
Results:
班长 地球亚洲中国北京
If you access a nonexistent key, you will get an error:
>>> info[' age ']traceback (most recent call last): File "<stdin>", line 1, in <module> Keyerror: ' Age '
When we are unsure whether a key exists in the dictionary and want to get its value, you can use the Get method and set the default value:
>>> age = Info.get ("Age") >>> Age # ' age ' key does not exist, so age is none>>> type (age) <type ' Nonetype ' >>>> age = info.get (' 18 ') # returns the default value if it does not exist in info 18>>> age18
4.2 Common operations for dictionaries 1
4.3 Common operations for Dictionaries 2
5 traversal
6 Public methods
Python built-in functions
Python contains the following built-in functions
Serial Number |
Method |
Description |
1 |
CMP (ITEM1, ITEM2) |
Comparison of two values |
2 |
Len (item) |
Count the number of elements in a container |
3 |
Max (item) |
Returns the maximum value of an element in a container |
4 |
MIN (item) |
Returns the element minimum value in the container |
5 |
Del (item) |
Delete a variable |
Cmp
>>> CMP ("Hello","Itcast")-1>>> CMP ("Itcast","Hello")1>>> CMP ("Itcast","Itcast") 0>>> CMP ([1, 2], [3, 4])-1>>> CMP ([1, 2], [1, 1])1>>> CMP ([1, 2], [1, 2, 3])-1>>> CMP ({"a": 1}, {"b": 1})-1>>> CMP ({"a": 2}, {"a": 1})1>>> CMP ({"a": 2}, {"a": 2,"b": 1})-1
Note: When CMP compares the dictionary data, it compares the keys before comparing the values.
Len
>>> Len ("hello itcast")12>>> len ([1, 2, 3, 4])4> >> Len ((3,4))2>>> len ({"a""b" : 2})2
Note: When Len is manipulating the dictionary data, it returns the number of key-value pairs.
Max
>>> Max ("Hello Itcast")'T'>>> Max ([1,4,522,3,4])522>>> Max ({"a": 1,"b": 2})'b'>>> Max ({"a": 10,"b": 2})'b'>>> Max ({"C": 10,"b": 2})'C'
Del
Del has two uses, one of which is del plus a space and the other is Del ()
>>> A = 1>>>a1>>>dela>>>Atraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'a' is notdefined>>> a = ['a','b']>>>delA[0]>>>a['b']>>>del(a)>>>Atraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'a' is notDefined
Multidimensional List/Ganso access
>>> tuple1 = [(2,3), (4,5)]>>> tuple1[0] (2,3)>>> tuple1[1]:0]2>>> tuple1[1]:2]traceback (most recent): File"<stdin>", line1,In <module>indexerror:tuple index out of range>>> tuple1[1]:1]3>>> tuple1[2][2]traceback (most recent): File"<stdin>", line1,in <module>indexerror:list index out of Range>> > tuple2 = tuple1+[(3)]>>> tuple2[(2, 3), (4, 5), 3]>>> tuple2[2]3>>> tuple2[2][ 0] Traceback (most recent): File 1, Span class= "Hljs-keyword" >in <module>typeerror: ' int ' object is not subscriptable
7 References
python-Basics-string-list-Ganso-Dictionary 2