1, list query

  • Index: Queries the position of the element in the list from left to right. If the element is found, the forward index of its first occurrence is returned. If it does not exist, an error is reported
  • Count: Queries the number of occurrences of the specified element in the list
  • In: Queries whether the specified element is in the list
  • Not in: Queries whether the specified element is not in the list
Select * from name_list;'Bob'.'Jack'.'Rose']

# print(name_list[0])  # Bob
# print(name_list[1])  # Jack
# print(name_list[2])  # Rose
# print(name_list[-1])  # Rose
# print(name_list[-2])  # Jack
# print(name_list[-3[) # Bob # Conclusion: The index in the list is exactly the same as in the string, # forward index from0To start, increment # negative index from left to right, starting with -1Select * from left where num_list = [; select * from left where num_list = [;1.2.3.4.5.6.7.8.5Print (num_list.index) print(num_list.index)5))  # 4
# ValueError: 9 is not in# print(num_list.index)9) # rindex does not have this method in the list # AttributeError:'list' object has no attribute 'rindex'
# print(num_list.rindex(5)) # find does not have this method in the list # AttributeError:'list' object has no attribute 'find'
# print(num_list.find(5Print (num_list.count(num_list.count(num_list.count)5))  # 2

# inCheck if the data element is in the list if it is True if it is not False #TypeError: argument of type 'int' is not iterable
# print(num_list in 5) # Use with careinOr notinData elements on the left, lists or other data sequences on the right5 in num_list)  # True
print(9 in num_list)  # False
# not inCheck if the data element is not in the list if it is not True if it is False print(5 not in num_list)  # False
print(9 not in num_list)  # True
Copy the code

2. List addition

  • Append: Appends data to the end of the class table
  • Extend: Iterates the data sequence to extract each element in turn and adds it to the end of the list
  • Insert: Appends data elements at the specified location
# append num_list = [1.2.3.4] # print1.2.3.4.5? # print(num_list.append(5)) # None # If you print the call to the append method directly, nothing will be printed. # List does not generate a new list when using the append method, but instead modiates the existing list num_list.append(5Print (num_list) # [print(num_list) #1.2.3.4.5]

# # str
# str1 = 'abc'# str2 = str1.replace() # str2 = str1.replace()'abc'.'cba') # print(str1) # print(str2) # extend1.Extend (data sequence) list1 = [1.2.3]
list2 = [4.5.6List1. Extend (list2) print(list1) # [1.2.3.4.5.6]
print(list2)  # [4.5.6] # append a sequence of strings, split the letters and place them in the list str1 ='itcast'
list2.extend(str1)
print(list2)  # [4.5.6.'i'.'t'.'c'.'a'.'s'.'t'] # what if the data in parentheses is not a data sequence? #TypeError: 'int'Object is not iterable must be an iterable in parentheses # list2.extend(4String types are iterable even if they contain only one value or an empty string. Lists, tuples, etc. are iterable even if they contain only data or an empty string. List2.extend ('3'Print (list2) # insert num_list = [1.2.3.4Insert (index of position to be inserted, object to be inserted) # The first parameter in insert is the index of position to be inserted, so if a number is inserted, the index of the data to be inserted is changed to the index shown in the first parameter # The original element and the subsequent element subscript +1In development, do not use insert # append to insert data unless it is clear that all index references have been modified. It is safer to insert data num_list.insert(1.5Print (num_list) # extend list1 = [1.2.3.4]
list2 = [5.6.7.8# list1. Append (list2) # [1.2.3.4[5.6.7.8]] # extend extend(list2) # [1.2.3.4.5.6.7.8]
print(list1)
Copy the code

3. Delete the list

  • Del searches for the elements in the list (using subscripts) and deletes them with del
  • Pop: Removes the element in the list at the specified subscript position. If not specified, the last element is deleted by default and the deleted value is returned
  • Remove: removes the subscript of the specified value, only the first occurrence of the value element from left to right
  • Clear: There is a logical difference between clearing a list and reassigning it to null.
List1 = [1.2.3.4]
# del list1
# NameError: name 'list1'# print(list1) # print(list1) # print(list1) # Get the current element by index and delete del list1[2]
# IndexError: list assignment index out ofRange # subscript index cannot exist when searching for data # del list1[9]
print(list1)  # [1.2.4] # if if can be deleted in the loop? Because I is a temporary variable, we use del to talk about I and2The reference relationship between list1 and2# is not deleted from the reference relationshipfor i in list1:
#     if i == 2: # del I # # print(list1) # pop1.2.3.4Print (list1.pop)2))
# IndexError: pop index out ofRange # print(list1.pop()12Print (list1) print(list1.pop()) print(list1.pop()) print(list1.pop() Print (list1) # remove (from left to right) list1 = [1.2.3.3.4.2.1] # delete from list2Will query the first encountered from left to right2Could not delete all of the classes in the table2
list1.remove(2)
print(list1)  # [1.3.3.4.2.1# remove will return the deleted content? Won't print (list1. Remove (3) # None # remove What happens if the deleted content does not exist? # list1.remove(123)  # ValueError: list.remove(x): x not inClear () print(list1) # [] print(list1) # [] print(list1) # []Copy the code

4. List modification

  • Modify with index: list [index] = new value
    • The query list index value must exist in the list
  • Reverse: Indicates the reverse of the list
  • Sort: Sort the list. Default is ascending
    • Reverse: Indicates that the list can be inverted or in descending order
    • Key: Add functions to make collation more complex and variable
List1 = [1.2.3.4Select * from list1; select * from list1;1] = 6# [1.6.3.4]
# IndexError: list assignment index out ofRange # fetch element position, must exist # list1[6] = 6Print (list1) # print(list1) Can't # list1 [(2.3)] = 6.7List1 [list1[2], list1[3] = 6.7Print (list1) # reverse list1 = [1.2.3.4Print (list1.reverse()) # None list1.reverse() print(list1) # [4.3.2.1] # sort list2 = [2.6.43.2.41.421[[sort]] # sort [sort] # sort # print(list2.sort()) # None # print(list2) # [2.2.6.41.43.421What if I want the list to be sorted in descending order? # list2.sort() # print(list2) # print(list2) # [421.43.41.6.2.2# list2.sort(reverse=True) # [421.43.41.6.2.2]
# print(list2)
Copy the code

List traversal

  • For traversal
  • While traversing
# whileThe function len() can query the length of the list. List1 = [12.123.1.1.1234.12.34.8]
# print(len(list1))
i = 0
while i < len(list1):
    print(list1[i])
    i += 1

# forTraversing the list # recommendedforLoop through container types (data sequences)for i in list1:
    print(i)
Copy the code

Nesting of lists

  • The nesting of other sublists within a list is the nesting of a list
  • Nested lists can be traversed using circular nesting
List nesting: include other list elements in a list name_list = [['Ming'.'little red'.'the little green'], ['Tom'.'Lily'.'Rose'], ['Joe'.'bill'.'Cathy'Print (name_list[) print(name_list[) print(name_list[) print(name_list[) print(name_list[)2Print (name_list[) print(name_list[) print(name_list[)2] [1]) # If we want to get every value in a nested list, what do we need to do? If you loop once, each loop yields the elements of the first level list, i.e., each sublistfor i inName_list: print(I) # If you want to print the nested list, you need to loop the nested listfor sub_list in name_list:
    for name inSub_list: print(name) # print(name) # print(nameCopy the code