Loop traversal of lists
1. Use the for loop
To output each data of the list more efficiently, you can use loops to complete it
demo:
namesList = ['xiaoWang','xiaoZhang','xiaoHua'] for name in namesList: print(name)
Result:
xiaoWang xiaoZhang xiaoHua
2. Use the while loop
To output each data of the list more efficiently, you can use loops to complete it
demo:
namesList = ['xiaoWang','xiaoZhang','xiaoHua'] length = len(namesList) i = 0 while i<length: print(namesList[i]) i+=1
Result:
xiaoWang xiaoZhang xiaoHua
List related operations
The data stored in the list can be modified, such as "add", "delete", "change".“
<1> Add elements ("add" append, extend, insert)
append
append lets you add elements to a list
demo:
#Define variable A with three elements by default A = ['xiaoWang','xiaoZhang','xiaoHua'] print("-----Before adding, list A Data-----") for tempName in A: print(tempName) #Prompt and add elements temp = input('Please enter the name of the student you want to add.:') A.append(temp) print("-----After adding, the list A Data-----") for tempName in A: print(tempName)
Results:
extend
You can add elements from another collection to the list one by one through extension
>>> a = [1, 2] >>> b = [3, 4] >>> a.append(b) >>> a [1, 2, [3, 4]] >>> a.extend(b) >>> a [1, 2, [3, 4], 3, 4]
insert
insert(index, object) inserts the element object before the specified position index
>>> a = [0, 1, 2] >>> a.insert(1, 3) >>> a [0, 3, 1, 2]
<2> Modify Elements ("Modify")
When modifying an element, the subscript is used to determine which element to modify before making the modification.
demo:
#Define variable A with three elements by default A = ['xiaoWang','xiaoZhang','xiaoHua'] print("-----Before modification, list A Data-----") for tempName in A: print(tempName) #Modifying elements A[1] = 'xiaoLu' print("-----After modification, the list A Data-----") for tempName in A: print(tempName)
Result:
-----Before modification, list A Data----- xiaoWang xiaoZhang xiaoHua -----After modification, the list A Data----- xiaoWang xiaoLu xiaoHua
<3> Find Elements ("Find" in, not in, index, count)
The so-called lookup is to see if a specified element exists.
in, not in
The common methods of searching in python are:
- in (exists), if it exists, then the result is true, otherwise it is false.
- not in, if not, then the result is true, otherwise false
demo
#List to be found nameList = ['xiaoWang','xiaoZhang','xiaoHua'] #Get the name the user is looking for findName = input('Please enter the name you want to find:') #Find out if it exists if findName in nameList: print('The same name was found in the dictionary.') else: print('Can't find')
Result 1: (Find)
Result 2: (Not found)
Explain:
If the in method is used, then not in is the same, but not in judges that it does not exist.
index, count
index and count are used in the same way as in strings
>>> a = ['a', 'b', 'c', 'a', 'b'] >>> a.index('a', 1, 3) # Note that the left closed right open interval Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'a' is not in list >>> a.index('a', 1, 4) 3 >>> a.count('b') 2 >>> a.count('d') 0
<4> Delete elements ("Delete" del, pop, remove)
Analogy in real life, if a classmate transferred, then the name of the student after this article should be deleted; in the development of this function is often used to delete.
The common deletion methods of list elements are:
- del: Delete by subscript
- pop: Delete the last element
- remove: Delete based on the value of the element
demo:(del)
movieName = ['Pirates of the Caribbean','Hacker empire','First Blood','Lord of the rings','The Hobbit','Fast & Furious'] print('------Before deleting------') for tempName in movieName: print(tempName) del movieName[2] print('------After deletion------') for tempName in movieName: print(tempName)
Result:
Before deletion Pirates of the Caribbean Hacker empire First Blood Lord of the rings The Hobbit Fast & Furious After deletion Pirates of the Caribbean Hacker empire Lord of the rings The Hobbit Fast & Furious
demo:(pop)
movieName = ['Pirates of the Caribbean','Hacker empire','First Blood','Lord of the rings','The Hobbit','Fast & Furious'] print('------Before deleting------') for tempName in movieName: print(tempName) movieName.pop() print('------After deletion------') for tempName in movieName: print(tempName)
Result:
Before deletion Pirates of the Caribbean Hacker empire First Blood Lord of the rings The Hobbit Fast & Furious After deletion Pirates of the Caribbean Hacker empire First Blood Lord of the rings The Hobbit
demo:(remove)
movieName = ['Pirates of the Caribbean','Hacker empire','First Blood','Lord of the rings','The Hobbit','Fast & Furious'] print('------Before deleting------') for tempName in movieName: print(tempName) movieName.remove('Lord of the rings') print('------After deletion------') for tempName in movieName: print(tempName)
Result:
Before deletion Pirates of the Caribbean Hacker empire First Blood Lord of the rings The Hobbit Fast & Furious After deletion Pirates of the Caribbean Hacker empire First Blood The Hobbit Fast & Furious
Sort (reverse)
The sort method is to rearrange the list in a specific order. The default is from small to large, and the parameter reverse=True can be changed to reverse order, from large to small.
The reverse method is to invert the list.
>>> a = [1, 4, 2, 3] >>> a [1, 4, 2, 3] >>> a.reverse() >>> a [3, 2, 4, 1] >>> a.sort() >>> a [1, 2, 3, 4] >>> a.sort(reverse=True) >>> a [4, 3, 2, 1]