Collection types Consistent with the concept of set in Mathematics , There is no order between set elements , Every Element uniqueness , There is no same element set element cannot be changed , It can't be a variable data type . Set with braces ‘{}’ Express , Separate the elements with commas to establish the collection type with Curly braces ‘{}’ or set(), Create an empty set type , You have to use set().
A = {"123", 456, (123, 456)} # Set up with parentheses >>>{456, (123, 456), '123'}
B = set("123456") # Use set Set up a collection >>>{'6', '2', '4', '5', '1', '3'}
C = {"123", 456, "123", 456} # The elements are unique and unordered >>>{456, '123'}
A = {123, "456", "p", "y"}
B = set("python")
print("A|B:", A | B)
print("A&B:", A & B)
print("A^B:", A ^ B)
print("A-B:", A - B)
# A|B: {'o', 'y', '456', 'p', 't', 'h', 'n', 123}
# A&B: {'p', 'y'}
# A^B: {'h', 't', 'o', 'n', '456', 123}
# A-B: {'456', 123}
Sequence It's a group of elements that have a precedence relationship . A sequence is a vector of one-dimensional elements , Element types can be different , Like the sequence of mathematical elements :S0, S1, … , Sn-1, Elements are guided by sequence numbers , Access specific elements of a sequence through subscripts .
Functions and methods describe len(s) Return sequence s The length of , That is, the number of elements min(s) Return sequence s The smallest element of ,s Medium elements need to be comparable max(s) Return sequence s The largest element of ,s Medium elements need to be comparable
s.index(x) or
s.index(x, i, j)
Return sequence s from i Start to j The first element in the position x The location of s.count(x) Return sequence s It appears that x The total number of timesTuples Is an extension of the sequence type . It's a kind of Sequence type and inherit all general operations of sequence type , Once created Cannot be modified , Use parentheses () or tuple() establish , Between elements Separate with commas . Brackets can be used or not .
A = (123, 456, 789)
B = (0x1100, 321, A)
print(A) # (123, 456, 789)
print(B) # (4352, 321, (123, 456, 789))
list yes Sequence type An extension of , Very often , After creation Can be modified at will , Use square brackets “[]” or list() establish , Elements are separated by commas . Each in the list Element types can be different , No length limit .
Functions or methods describe ls[i]= x Replace list ls The first i Element is xls[i: j: k] = lt Use list lt Replace ls Sub list of corresponding elements after slicing del ls[i] Delete list ls pass the civil examinations i Elements del ls[i: j: k] Delete list ls pass the civil examinations i To the first j With k Element for step size ls + = lt Update list ls, Will list lt Element added to list ls in ls *= n Update list ls, Its elements repeat n Time
ls = ["python", "java", "C Language "]
ls[1:2] = ["C++"]
print(ls) # ['python', 'C++', 'C Language ']
print(ls*2) # ['python', 'C++', 'C Language ', 'python', 'C++', 'C Language ']
ls = ["tiger", "dog", "cat"]
ls.append("mouse")
print(ls) # ['tiger', 'dog', 'cat', 'mouse']
ls.insert(4, "fish")
print(ls) # ['tiger', 'dog', 'cat', 'mouse', 'fish']
ls.reverse()
print(ls) # ['fish', 'mouse', 'cat', 'dog', 'tiger']
lt = [] # Define an empty list
lt += [1, 2, 3, 4] # Add four elements
lt[2] = 5 # Mark the subscript as 2 The element of is assigned the value of 5
lt.insert(2, 6) # The subscript for 2 The value inserted before the element is 6 The elements of
del lt[2] # Delete the subscript as 2 The element value of
del lt[1:3] # Delete subscript 1 to 2 The element value of
print(" The current list contains numbers 0") if 0 in lt else print(" The current list does not contain numbers 0") # Whether the current list contains numbers 0
lt.index(4) # Get the subscript 4 The element value of
len(lt) # Current list length
max(lt) # Maximum value of current list
lt.clear() # clear list
Dictionary type yes “ mapping ” The embodiment of , Key value pair : Key is an extension of data index . A dictionary is a collection of key value pairs , Between key value pairs disorder . use Braces and dict() establish , Use colons for key value pairs : Express {< key 1>:< value 1>,< key 2>:< value 2>, ... ,< key n> :< value n>}.
d = {1: " Xiao Ming ", 2: " Zhang San ", 3: " Li Si "}
print(d) # {1: ' Xiao Ming ', 2: ' Zhang San ', 3: ' Li Si '}
print(d[1]) # Xiao Ming
print(type(d)) # <class 'dict'>
d = {1: " Xiao Ming ", 2: " Zhang San ", 3: " Li Si "}
print(1 in d) # True
print(d.keys()) # dict_keys([1, 2, 3])
print(d.values()) # dict_values([' Xiao Ming ', ' Zhang San ', ' Li Si '])
print(d.items()) # dict_items([(1, ' Xiao Ming '), (2, ' Zhang San '), (3, ' Li Si ')])
Functions and methods describe d.get(k,<default>) key k There is , Then return the corresponding value , If not, return to <default> value d.pop(k,<default>) key k There is , Then take out the corresponding value , If not, return to <default> value d.popitem() Random from Dictionary d Take out a key value pair , Returns... As a tuple d.clear() Delete all key value pairs len(d) Return dictionary d The number of elements in d = {} # Define an empty dictionary
d[" name 1"] = " Zhang San " # Add two key value pair elements
d[" name 2"] = " Li Si "
d[" name 3"] = " Wang Wu "
d[" name 2"] = " Li San " # Change key to “ name 2” The elements of
print(" name 2 The key is in the current dictionary ") if " name 2" in d else print(" name 2 The key is not in the current dictionary ") # Judge “ name 2” Is it in the current dictionary
print(len(d)) # Dictionary length
d.clear() # Empty dictionary