A dictionary is a mutable container model and can store any type of object
Characteristics:
The dictionary is unordered, it cannot be accessed by an offset, only by a key.
internal no order, through the key to read the content, can be nested, merge, convenient for us to organize a variety of data structures, and can modify the contents inside, belong to the mutable type.
The keys that make up the dictionary must be immutable data types, such as numbers, strings, tuples, and so on, and mutable objects such as lists cannot be keys
1. Definition of Dictionary
A = {' 1 ': ' A ', ' 2 ': ' B ', ' 3 ': ' C '}
Huo
A = Dict (1= ' A ', 2= ' B ', 3= ' C ')
2. Insert Content
A[4] = ' d '
The result is: {' 1 ': ' A ', ' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}
3. Modify the Content
a[' 1 '] = ' q '
The result is: {' 1 ': ' Q ', ' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}
4. Delete Content
Del (a[' 1 '])
The result is: {' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}
Common methods:
Dict.keys () View dict keys
Divt.values () View all the values in the Dict
Dict.items ()
Dict.clear () Delete all the elements in the dictionary
Dict.pop ()
Dict.get (' key ') gets the value of a key from the dictionary
Dict.has_key (' key ')
Collection
The collection is divided into mutable set (set) and immutable set (Frozenset)
Set hash, Frozenset non-hash (infrequently used)
Set is a non-repeating set of elements that support Union (union), intersection (intersection), difference (differential set), and sysmmetric difference (symmetric difference sets), and other mathematical operations
1. Definition set (SET)
A = set (' Aasdfsdfa ')
The result is: {' A ', ' d ', ' f ', ' s '}
2. Seeking Union (Union)
A = set (' abc ') b = Set (' BCD ') c = a|b
The result is: {' A ', ' B ', ' C ', ' d '}
3. Seeking intersection (intersection)
c = a&b
The result is: {' B ', ' C '}
4, Seek difference (difference set)
c = A-B
The result is: {' A '}
Common methods:
Set.add (obj) adds a value
Set1.difference (Set2) Seeking difference set
Set1.union (Set2) seeking union
Set1.intersection (Set2) to seek the intersection
Set.remove (obj) removing obj
Set.clear ()
Set.pop ()
Python dictionaries, collections