Sort several data structures in Python, including lists, dictionaries, tuples, sets, and python data structures.
List: shoplist = ['apple', 'mango', 'carrot', 'bana']
Dictionary: di = {'A': 123, 'B': 'something '}
Set: jihe = {'apple', 'pear ', 'apple '}
Tuples: t = 123,456, 'Hello'
1. List
Empty list: a = []
Function Method: a. append (3) >>> [3]
A. extend ([3, 4, 5]) >>> [3, 3, 4, 5] Add a list Sequence
A. insert (1, 'Hello') >>> [3, 'Hello', 3,4, 5]
A. remove (3) >>> ['hello', 3,4, 5] Delete the first occurrence 3. If there is no 3, an error is returned.
A. pop () >>> ['hello', 3,4]
A. pop (0) >>> [3, 4]
A. index (4) >>> 1 returns the subscript of the first 4 that appears.
A. count (3) >>> 1 Number of element 3 in the list
A. sort> [3, 4] sorting
A. reverse () >>> [4, 3] reverse Order
How to delete an element
A. remove (3) delete an element by using a value. Delete the first element that is a parameter value.
A. pop () deletes an element by subscript. By default, the last value of the List is deleted. If a parameter is included, the element whose subscript is the parameter value is deleted.
Del a [0] deletes an element by subscript,
Del a [] deletes an element whose subscript is 2 and 3 in table.
Del a [:] delete all elements in List
Del a delete list
List derivation:
Vec = [2, 4, 6]
[3 * x for x in vec if x <6]> [6, 12] 3*2, 3*4
Vec2 = [1, 2, 3]
[X * y for x in vec for y in vec2]> [2, 4, 6, 4, 8, 12, 6, 12, 18]
Derivation of nested list:
Mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Print ([[row [I] for row in mat] for I in [0, 1, 2])
>>> [[1, 4, 7], [2, 5, 8], [3, 6, 9]
Thinking: What are the differences between list (zip) and list (* mat )?
2. tuples
Null tuples: t = ()
Tuples assignment: t = (123,345)
T [0]> 123
3. Dictionary
D = {'jack': 'Jack @ mail.com ', 'Tom': 'Tom @ main.com '}
D ['jack']> 'Jack @ mail.com
D ['Jim '] = 'Jim @ sin.com' >>>> {'Jim ': 'Jim @ sin.com', 'jack': 'Jack @ mail.com ', 'Tom ': 'Tom @ main.com '}
Del d ['Jim '] >>>{ 'jack': 'Jack @ mail.com', 'Tom ': 'Tom @ main.com '}
List (d. keys () returns an unordered list composed of all the keywords in the dictionary.
Sorted (d. keys () returns a list of all the keywords in the dictionary.
Dict () constructor can create a dictionary directly from the key-value Pair
Dict ([('time', 123), ('tiny', 234)]) >>>{ 'tiny': 234, 'Tim': 123}
Derivative dictionary creation:
{D2: d2 + '@ main.com' for d2 in list (d. keys ())}
>>>{ 'Jack': 'Jack @ main.com ', 'Tom': 'Tom @ main.com '}
Exercise: cyclically output the key-value pairs in the dictionary:
For name, email in d. items ():
Print (name, email)
4. Set
Empty set: A = set () ※to create an empty set, you must use set ()
Demo:
Basket = {'apple', 'Orange ', 'apple'} >>>{ 'Orange', 'apple'} note that only one duplicate element is displayed?
'Apple' in basket >>> True
'Pear 'in basket >>> False
Mathematical operations of a set:
A = set ('abcdabca') >>>{ 'C', 'B', 'A', 'D '}
B = {'A', 'B', 'M'} >>>{ 'B', 'A', 'M '}
A-B >>>{ 'C', 'D '}
B-a >>>{ 'M '}
A | B >>>{ 'C', 'D', 'B', 'A', 'M '}
A & B >>>{ 'A', 'B '}
A ^ B >>>{ 'C', 'D', 'M '}
Set derivation:
{X for x in a if x not in 'AB'} >>>{ 'C', 'D '}