List: shoplist = [' Apple ', ' Mango ', ' carrot ', ' banana ']
Dictionary: di = {' a ': 123, ' b ': ' something '}
Collection: Jihe = {' Apple ', ' pear ', ' apple '}
Tuple: 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 of 3, No 3 is an error
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) Number of elements 3 in >>>1 list
A.sort >>>[3,4] Sort
A.reverse () >>>[4,3] Reverse order
Methods for deleting elements
A.remove (3) Delete an element by value, delete the first argument that is worth the element
A.pop () Deletes an element by default, deletes the last value of the list, with a parameter removes the element labeled as the parameter value
del a[0]Delete the element by subscript,
Del a[2:4] Delete the element under the a table labeled 2,3
Del a[:] Delete all elements of a list
Del a delete list
List Deduction Formula:
VEC = [2,4,6]
[3*x for x in VEC if x<6] >>>[6,12] 3*2,3*4
VEC2 = [a]
[x*y for x in Vec for y in vec2] >>>[2,4,6,4,8,12,6,12,18]
Nested-list derivation:
Mat = [
[a],
[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]]
Think: What is the difference between the list (zip (mat)) and list (zip (*mat)) results
2. tuple---------------------------------------------------------------------------------------------------
Empty tuple: t = ()
Tuple assignment: t = (123,345)
t[0] >>>123
3. Dictionary ---------------------------------------------------------------------------------- -----------------------
d = {' Jack ': ' [email protected] ', ' Tom ': ' [email protected] '}
d[' Jack '] >>> ' [email protected]
& nbsp; d[' Jim '] = ' [email protected] ' >>>{' Jim ': ' [email protected] ', ' Jack ': ' [email protected] ', ' Tom ': ' [email protected] '}
del d[' Jim ' >>>{' Jack ': ' [email protected] ', ' Tom ': ' [email protected] '}
List (d.keys ()) Returns an unordered list of all the keywords in a dictionary
Sorted (d.keys ()) Returns a sorted list of all the keywords in a dictionary
The Dict () constructor can create a dictionary directly from the key-value pair
Dict ([' Tim ', 123), (' Tiny ', 234)]) >>>{' Tiny ': 234, ' Tim ': 123}
Derivation creates a dictionary:
{d2:d2+ ' @main. com ' for D2 in list (d.keys ()}}
>>>{' Jack ': ' [email protected] ', ' Tom ': ' [email protected] '}
Exercise: a Key-value pair in a circular output dictionary:
For Name,email in D.items ():
Print (name,email)
4. collection--------------------------------------------------------------------------------------------
Empty collection: A = set () ※ To create an empty collection, you must use set ()
Demonstrate:
Basket = {' Apple ', ' Orange ', ' Apple '} >>>{' Orange ', ' Apple '} Note that duplicate elements only show one?
' Apple ' in basket >>>true
' Pear ' in basket >>>false
Mathematical operations of the Set:
A = set (' ababcdabca ') >>>{' 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 '}
Python data structures: lists, dictionaries, tuples, Collections