Continued: http://www.cnblogs.com/liu-wang/p/8973273.html
3-tuple
4 dictionaries
4.1 Introduction to Dictionaries
<2> Dictionary in software development
The variable info is of dictionary type:
info = {'name':'班长', 'id':100, 'sex':'f', 'address':'地球亚洲中国北京'}
illustrate:
- Dictionaries, like lists, can also store multiple pieces of data
- When finding an element in a list, it is based on the subscript
- When looking for an element in the dictionary, it is based on the 'name' (that is, the value in front of the colon: such as 'name', 'id', 'sex' in the above code)
- Each element of the dictionary consists of 2 parts, key:value. For example 'name': 'squad leader', 'name' is the key, and 'squad leader' is the value
<3> Access the value according to the key
info = {'name':'Monitor', 'id':100, 'sex':'f', 'address':'Earth Asia China Beijing' }
print(info['name'])
print(info['address'])
result:
班长
地球亚洲中国北京
If you access a non-existent key, you will get an error:
>>> info['age']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'age'
When we are not sure if a key exists in the dictionary and want to get its value, we can use the get method, and we can also set a default value:
>>> age = info.get('age')
>>> age #'age' key does not exist, so age is None
>>> type(age)
<type 'NoneType'>
>>> age = info.get('age', 18) # If the key 'age' does not exist in info, return the default value of 18
>>> age
18
4.2 Common operations of dictionaries 1
4.3 Common operations on dictionaries 2
5 Traversal
6 public methods
python built-in functions
Python includes the following built-in functions
serial number | method | describe |
---|---|---|
1 | cmp(item1, item2) | compare two values |
2 | len (item) | Count the number of elements in the container |
3 | max(item) | Returns the maximum value of the element in the container |
4 | min(item) | Returns the minimum value of the elements in the container |
5 | del(item) | delete 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 dictionary data, it first compares the keys, and then compares the values.
len
>>> len("hello itcast") 12 >>> len([1, 2, 3, 4]) 4 >>> len((3,4)) 2 >>> len({"a":1, "b":2}) 2
Note: When len operates 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'
the
There are two ways to use del, one is del plus space, and the other is del()
>>> a = 1 >>> a 1 >>> del a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> a = ['a', 'b'] >>> del a[0] >>> a ['b'] >>> del(a) >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined
Multidimensional list/tuple access
>>> tuple1 = [(2,3),(4,5)] >>> tuple1[0] (2, 3) >>> tuple1[0][0] 2 >>> tuple1[0][2] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> tuple1[0][1] 3 >>> tuple1[2][2] Traceback (most recent call last): File "<stdin>", line 1, 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 call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not subscriptable
7 Quotes