Basic knowledge
day6 - List
01. Slice of list
1. Slice - get some elements
The slice result is a list
Syntax: list [start subscript: end subscript: step size]
explain:
Start subscript - subscript value (0 and - 1 are OK); Determine the starting point of the effective range of the slice, which can be obtained
: - Fixed writing
End subscript - subscript value (starting from 0 and starting from - 1): determine the end point of the effective range of the slice, which cannot be obtained
Step size - 1) direction of slice (the direction corresponding to the step size must be consistent with the direction from start to end, otherwise the slice result is empty)
- 2) Determine the direction of the slice and how to get the elements (one by one or jump)
notes:Valid range of slices:[Start subscript,End subscript]
games = ['Glory of Kings','Game for Peace','Resident Evil','Original God','legend of chusen','Cross Fire','Tribal conflict','Red police','Contra'] print(games[1:5:1]) # ['peace elite', 'biochemical crisis',' original God ',' kill immortal '] print(games[-1:-6:-1]) # ['soul fighting', 'red police', 'tribal conflict', 'crossing the line of fire', 'killing Immortals'] print(games[-1:-5:1]) # [] print(games[0:-3:2]) # ['glory of the king', 'peace elite', 'biochemical crisis',' original God ',' killing Immortals', 'crossing the line of fire'] print(games[-1:2:-2]) # ['soul fighting', 'tribal conflict', 'killing Immortals'] print(games[2:-1:1]) # ['biochemical crisis',' original God ',' killing Immortals', 'crossing the line of fire', 'tribal conflict', 'red police'] print(games[-1:-3:-1])
2. Omit step size
List [start subscript: end subscript]
Omit the step, the step is 1
print(games[2:-2])
3. Omit the starting subscript
List [: end subscript: step] / list [: end subscript]
Omit the starting subscript. If the step size is an integer, take it back from the first element; If the step size is negative, take it forward from the last element
print(games[:-3]) print(games[:3:-1])
4. Omit end subscript
List [start subscript:: step] / list [start subscript:]
Omit end subscript: if the step size is positive, take the last element from the start subscript; if the step size is negative, take the first element from the start subscript
print(games[1:])
print(games[3::2])
print(games[-2::-1])
5. Save together
List [:: step] / list [:]
print(games[:])
02. Deletion and modification
1. Delete - delete list elements (reduce the number of list elements)
Note: the subscript cannot be out of bounds
1) del list [subscript] - deletes the element corresponding to the specified subscript in the list
teleplays = ['Nirvana in Fire','Roman Empire','The temptation of home','Kangxi Dynasty','Two Broke Girls','Bright sword','The Big Bang Theory','Journey to the West'] del teleplays[2] print(teleplays) del teleplays[-2] print(teleplays)
2) List Remove - deletes the specified element from the list
Note: a. if the element does not exist, an error will be reported
b. If there are multiple elements, delete only the first one
teleplays.remove('Nirvana in Fire') print(teleplays) # ['the great Qin Dynasty', 'Kangxi Dynasty', 'bankrupt sisters',' shining sword ',' journey to the West '] # teleplays. Error in remove ('legend of white snake ') nums = [10,20,30,20,10] nums.remove(20) print(nums) #[10, 30, 20, 10]
3)
List pop() - fetch the last element of the list
List Pop (subscript) - retrieves the element corresponding to the specified subscript in the list
teleplays = ['Nirvana in Fire','Roman Empire','The temptation of home','Kangxi Dynasty','Two Broke Girls','Bright sword','The Big Bang Theory','Journey to the West'] pop_item=teleplays.pop() print(teleplays) print(pop_item) pop_item = teleplays.pop(1) print(teleplays) print(pop_item)
4) List clear() - clear the list
teleplays = ['langyabang', 'Daqin', 'temptation to go home', 'Kangxi Dynasty', 'bankrupt sisters',' bright sword ',' big bang of life ',' journey to the West ']
teleplays.clear()
print(teleplays)
2. Modify - modify the value of the element
List [subscript] = value - modifies the element corresponding to the specified subscript in the list to the specified value
teleplays = ['langyabang', 'Daqin', 'temptation to go home', 'Kangxi Dynasty', 'bankrupt sisters',' bright sword ',' big bang of life ',' journey to the West ']
print(teleplays)
teleplays[0] = '
practice:Change all scores below 60 to 0 scores = [90,45,56,89,76,56,92,45,30,59,67,70] for x,y in enumerate(scores): if y < 60: scores[x]=0 print(scores)
Exercise 2: delete scores below 60 from the list scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70] lst=[] for x in scores: if x >= 60: lst.append(x) scores=lst print(scores)
03. List related operations
1. Mathematical operators: +*
List 1 + list 2 - merge list 1 and list 2 into a new list
List 1N / nlist 1 - merge N list 1 to produce a new list
print([1,2,3]+[10,20,30]) # [1, 2, 3, 10, 20, 30] lst1=[100,200] lst2=[1000,2000] print(lst1+lst2,lst1) # [100, 200, 1000, 2000] [100, 200] print(lst1*3) # [100, 200, 100, 200, 100, 200] list
2. Comparison operators = =,! =, >. <, ><=
1)==,!=
print([1,2,3][1,2,3]) # True
print([1,2,3][1,3,2]) # False
2) LIST1 > (<, > =. < =) List2
The principle of comparing the size of two lists: compare the size of the first pair of unequal elements (the elements with the same subscript in two lists are a pair)
print([10,2,-3,4,5] > [10,2]) # True
3.in and not in
Element in list
Element not in list
print(10 in[10,20,30]) # True print([10,20] in [10,20,30]) # False print([10,20] in [[10,20],30]) # Tru v = 34 if type(v) in [int,float,complex,bool]: print('It's a number') else: print('Not a number')
04. Correlation functions and methods
1. List related methods copy,count,index,reverse,sort
1) List Count (element) - counts the number of specified elements in the list
nums = [10,20,30,4,10,20,10,10] print(nums.count(10)) # 4 print(nums.count(100)) # 0
2) List Index (element) - gets the subscript of the element that appears in the list for the first time
print(nums.index(10)) # 0
print(nums.index(20)) # 1
print(nums.index(4)) # 3
3) List reverse() - reverse order
nums = [10,20,5,2,100]
nums.reverse()
print(nums)
4)
List sort() - sort the elements in the list from small to large
List sort(reverse=True) - sorts the elements in the list from large to small
nums = [10,20,5,2,100] nums.sort() print(nums) # [2, 5, 10, 20, 100]
#5) list copy() - as like as two peas, the new list is exactly the same.
task
1. Given a list of numbers, find the central element of the list.
list1=[1,2,3,5] num=sum(list1)//len(list1) if len(list1)%2==0: print(list1[num]) else: print(list1[num-1])
2. Given a list of numbers, find the sum of all elements.
list2=[1,2,3,5,9,11] print(sum(list2))
3. Given a list of numbers, output all odd subscript elements.
list3=[10,20,30,40,60,80] for x,y in enumerate(list3): if x%2!=0: print(y,end=',')
4. Given a list of numbers, output the elements with odd values among all elements.
list4=[11,22,35,42,60,81] for x,y in enumerate(list4): if y%2!=0: print(y,end=',')
5. Given a list of numbers, multiply all elements by two.
For example: num = [1, 2, 3, 4] - > num = [2, 4, 6, 8]
list5=[5,6,8,12,45,101] for x in list5: print(x*2)
6. There is a list with a length of 10. There are 10 names in the array. It is required to remove the duplicate names
For example: names = ['Zhang San', 'Li Si', 'rhubarb', 'rhubarb', 'Zhang San', 'Zhang San', 'Zhang San'] - > Names = ['Zhang San', 'Li Si', 'rhubarb']
list6=['Zhang San', 'Li Si', 'chinese rhubarb', 'chinese rhubarb', 'Zhang San', 'Zhang San', 'Zhang San','Wang Wu','Zhao Liu'] list_name=list6 k=0 for x in list6: k=0 for y in list_name: if x==y: k+=1 if k==2: list6.remove(x) k=1 print(list6)
7. Use a list to save all scores of a program and calculate the average score (remove the highest score, remove the lowest score and calculate the final score)
list7=[92,100,80,99,98,97,96,95] list7.sort() del list7[0] max=len(list7) del list7[max-1] print(sum(list7)/len(list7))
8. There are two lists A and B. use list C to obtain the common elements in the two lists
For example: A = [1 'a', 4, 90] B = ['a', 8 'j', 1] -- > C = [1 'a']
A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] C=[] for x in A: for y in B: if x==y: C.append(x) print(C)
9. There is a list of numbers to get the maximum value in this list (Note: the max function cannot be used)
For example: num = [19, 89, 90, 600, 1] - > 600
nums = [19, 89, 90, 600, 1] nums.sort() max = (nums[len(nums)-1]) print(max)
10. Get the most frequent element in the list
For example: nums = [1, 2, 3,1,4,2,1,3,7,3,3] - > Print: 3
nums = [1, 2, 3,1,4,2,1,3,7,3,3] times=[] for x in nums: if x not in times: times[x]=nums.count(x) max1=max(times.values()) for y,z in times.iteams: