当前位置:网站首页>Day06 list job
Day06 list job
2022-07-04 10:38:00 【She was your flaw】
list
One 、 List slice
1.1 section - Get some elements
The result of slicing is a list
grammar : list [ Start subscript : End subscript : step ]
explain :
Start subscript - Subscript value (0 Starting and -1 Anything at the beginning ); It is the starting point for determining the effective range of slices , You can get it.
: - Fixed writing
End subscript - Subscript value (0 Starting and -1 Anything at the beginning ); Determine the end point of the slice effective range , Can't get
step - 1) Determine the direction of the slice ( The direction corresponding to the step size must be consistent with the direction from start to end )
2) How to get elements ( Take one by one or jump )
Be careful : The valid range of slices is :[ Start subscript , End subscript )
games = [' Glory of Kings ', ' Game for Peace ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print(games[1:5:1]) # [' Game for Peace ', ' Protogod ', ' , ', ' Crossing the line of fire ']
print(games[1:-1:1]) # [' Game for Peace ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ']
print(games[3:-1:-1]) # []
print(games[-1:-5:1]) # []
print(games[0:-3:2]) # [' Glory of Kings ', ' Protogod ', ' Crossing the line of fire ']
print(games[-1:2:-2]) # [' Contra ', ' Tribal conflict ', ' , ']
print(games[2:-1:1]) # [ Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ']
1.2 Omit step size
list [ Start subscript : End subscript ] == [ Start subscript : End subscript :1]
Omit step size , Step size is 1
games = [' Glory of Kings ', ' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print(games[2:-2]) # [' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ']
1.3 Omit start subscript
list [: End subscript : step ] / list [: End subscript ]
Omit start subscript , If the step size is positive , Start with the first element and take it back ; If the step size is negative , Start with the last element and move forward
games = [' Glory of Kings ', ' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print(games[:-3]) # [' Glory of Kings ', ' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ']
print(games[:3:-1]) # [' Contra ', ' Red Alert ', ' Tribal conflict ', ' Crossing the line of fire ', ' , ']
1.4 Omit the closing subscript
list [ Start subscript :: step ] # Omit the closing subscript
list [ Start subscript :] # Omit the end mark and step
Omit the closing subscript : If the step size is positive , Take the last element from the beginning subscript ; If the step size is negative , Take the first element from the starting subscript
games = [' Glory of Kings ', ' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print(games[1:]) # [' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print(games[3::2]) # [' Protogod ', ' Crossing the line of fire ', ' Red Alert ']
print(games[-2::-1]) # [' Red Alert ', ' Tribal conflict ', ' Crossing the line of fire ', ' , ', ' Protogod ', ' Biochemical crisis ', ' Game for Peace ', ' Glory of Kings ']
1.5 Together province
list [:: step ] # If the step size is positive , From the first element to the last element ; If the step size is negative , From the last element to the first element
list [:] # Take the whole list
print(games[::-1]) # [' Contra ', ' Red Alert ', ' Tribal conflict ', ' Crossing the line of fire ', ' , ', ' Protogod ', ' Biochemical crisis ', ' Game for Peace ', ' Glory of Kings ']
print(games[::2]) # [' Glory of Kings ', ' Biochemical crisis ', ' , ', ' Tribal conflict ', ' Contra ']
print(games[:]) # [' Glory of Kings ', ' Game for Peace ', ' Biochemical crisis ', ' Protogod ', ' , ', ' Crossing the line of fire ', ' Tribal conflict ', ' Red Alert ', ' Contra ']
print('abcdef'[1:])
print('abcdef'[::2])
Two 、 Deletion and modification
1.1 Delete - Delete list elements ( Reduce the number of list elements )
teleplays = [‘ List of reed {langya} ’, ‘ Daqin ’, ‘ The temptation of home ’, ‘ Kangxi Dynasty ’, ‘ Two Broke Girls ’, ‘ Strong-arm reaction ’, ‘ The big bang theory ’, ‘ Journey to the west ’]
- del list [ Subscript ] - Delete the element corresponding to the specified subscript in the list
Be careful : Subscript cannot be out of bounds
del teleplays[2]
print(teleplays) # [' List of reed {langya} ', ' Daqin ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' The big bang theory ', ' Journey to the west ']
del teleplays[-2]
print(teleplays) # [' List of reed {langya} ', ' Daqin ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' Journey to the west ']
- list .remove( Elements ) - Deletes the specified element in the list
Be careful :a. If the element does not exist , Will report a mistake
b. If the element has more than one , Delete only the first
teleplays.remove(' List of reed {langya} ')
print(teleplays) # [' Daqin ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' Journey to the west ']
teleplays.remove(' The white snake ') # ValueError: list.remove(x): x not in list
nums = [10, 20, 30, 20, 10, 20]
nums.remove(20)
print(nums) # [10, 30, 20, 10, 20]
- list . pop() - Take out the last element of the list
list .pop( Subscript ) - Take out the element corresponding to the specified subscript in the list
teleplays = [' List of reed {langya} ', ' Daqin ', ' The temptation of home ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' The big bang theory ', ' Journey to the west ']
del_item = teleplays.pop()
print(teleplays)
print(del_item) # Deleted ' Journey to the west '
4). list .clear() - List is empty
teleplays = [' List of reed {langya} ', ' Daqin ', ' The temptation of home ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' The big bang theory ', ' Journey to the west ']
teleplays.clear()
print(teleplays)
teleplays = [' List of reed {langya} ', ' Daqin ', ' The temptation of home ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' The big bang theory ', ' Journey to the west ']
print(teleplays)
teleplays = []
print(teleplays)
- Change - Modify the value of an element
list [ Subscript ] = value - Modify the element corresponding to the specified subscript in the list to the specified value
teleplays = [' List of reed {langya} ', ' Daqin ', ' The temptation of home ', ' Kangxi Dynasty ', ' Two Broke Girls ', ' Strong-arm reaction ', ' The big bang theory ', ' Journey to the west ']
print(teleplays)
teleplays[0] = ' Celebrate more than '
print(teleplays)
practice : Will be lower than 60 All the scores are changed to 0 branch
Method 1 :
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
for index in range(len(scores)):
if scores[index] < 60:
scores[index] = 0
print(scores)
Method 2 :
for index, item in enumerate(scores):
if item < 60:
scores[index] = 0
print(scores)
Method 3 :
scores = [90, 45, 56, 89, 76, 56, 92, 45, 30, 59, 67, 70]
new_scores = []
for item in scores:
if item < 60:
new_scores.append(0)
continue
new_scores.append(item)
print(new_scores)
3、 ... and 、 List related operations
- Numeric operators :+、*
list 1 + list 2 – Will list 1 And list 2 Merge into a new list
list 1 * N / N * list 1 – take N A list 1 Merge to produce a new list
print([1, 2, 3]+[10, 20, 30])
list1 = [100, 200]
list2 = [1000, 2000]
print(list1 + list2, list1)
print(list1 * 3) # [100, 200, 100, 200, 100, 200]
- Comparison operator :、!=、>、<、>=、<=
1)、!=
print([1, 2, 3] == [1, 2, 3])
print([1, 2, 3] == [1, 3, 2])
print({
1, 2, 3} == {
1, 3, 2})
2) list 1 > (<、>=、<=) list 2
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 , This subscript is 0 The starting subscript )
print([1, 200, 300, 400, 500] > [10, 2]) # False
print([11, 0, 0, 0] > [10, 2]) # True
- in and not in
Elements in list - Determine if a specified element exists in the list
Elements not in list - Determine whether the specified element does not exist in the list
print(10 in [10, 20, 30]) # True
print([10, 20] in [10, 20, 30]) # False
print([10, 20] in [[10, 20], 30]) # True
practice : Judge whether the score is 100 Points or 0 Points or 60
score = 89
if score == 100 or score == 0 or score == 60:
print(' Special values ')
if score in [0, 60, 100]:
print(' Special values ')
v = 34
if type(v) == int or type(v) == float or type(v) == complex or type(v) == bool:
print(' Numbers ')
else:
print(' Not numbers ')
if type(v) in [int, float, complex, bool]:
print(' Numbers ')
else:
print(' Not numbers ')
5、 ... and 、 Correlation functions and methods
- List related methods :copy、count、index、reverse、sort
1) list .count( Elements ) - Count 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(4)) # 1
print(nums.count(100)) # 0
- list .index( Elements ) - Get the subscript of the element that appears in the list for the first time ( The subscript is 0 Starting subscript value )
print(nums.index(10)) # 0
print(nums.index(20)) # 1
print(nums.index(4)) # 3
# print(nums.index(100)) # ValueError: 100 is not in list( Not found in the list 100)
- . list .reverse() - In reverse order
nums = [10, 20, 5, 2, 100]
nums.reverse()
print(nums) # [2, 5, 20, 10]
4). list .sort() - Sort the elements in the list from small to large
list .sort(reverse=True) - Sort the elements in the list from large to small
nums = [10, 20, 5, 2, 100]
nums.sort()
print(nums) # [2, 5, 10, 20, 100]
nums = [10, 20, 5, 2, 100]
nums.sort(reverse=True)
print(nums) # [100, 20, 10, 5, 2]
- list .copy() - Copying the list produces a new list with exactly the same elements
list1 = [10, 20, 30]
print(list1) # [10, 20, 30]
list2 = list1
print(list2) # [10, 20, 30]
list3 = list1.copy()
print(list3) # [10, 20, 30]
list1.append(100)
print(list2) # [10, 20, 30, 100]
print(list1) # [10, 20, 30, 100]
print(list3) # [10, 20, 30]
List job
1. Know a list of numbers , Find the central element of the list .
nums = [12, 32, 73, 5, 83, 66, 52]
count = len(nums)
if count % 2:
print(nums[count//2])
else:
index = count//2
print(nums[index//2-1], nums[index//2])
2. Know a list of numbers , Sum all elements .
nums = [2, 23, 5, 67, 87, 76, 9, 4]
sum1 = 0
for item in nums:
sum1 += item
print(sum1)
3. Know a list of numbers , Output all odd subscript elements .
nums = [2, 23, 5, 67, 87, 76, 9, 4]
print(nums[1::2])
4. Know a list of numbers , Output all elements , Elements with odd values .
nums = [2, 23, 5, 67, 87, 76, 9, 4]
for item in nums:
if item % 2 == 1:
print(item)
5. Know a list of numbers , Multiply all elements by two .
for example :nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums = [2, 23, 5, 67, 87, 76, 9, 4]
new_nums = []
for item in nums:
new_nums.append(item * 2)
print(new_nums)
6. One length is 10 A list of , There are... In the array 10 Personal name , Ask to get rid of the duplicate
for example :names = [‘ Zhang San ’, ‘ Li Si ’, ‘ rhubarb ’, ‘ rhubarb ’, ‘ Zhang San ’, ‘ Zhang San ’, ‘ Zhang San ’] -> names = [‘ Zhang San ’, ‘ Li Si ’, ‘ rhubarb ’]
names = [2, 2, 5, 87, 87, 76, 7, 76]
new_names = []
for name in names:
if name not in new_names:
new_names.append(name)
print(new_names) # [2, 5, 87, 76, 7]
7. Use a list to save all the scores of a program , Find the average score ( Remove one of the highest scores , Remove a minimum score , Find the final score )
scores = [82, 83, 85, 77, 87, 76, 90, 84]
max_score = min_score = scores[0]
sum1 = 0
for item in scores:
if item > max_score:
max_score = item
elif item < min_score:
min_score = item
sum1 += item
print((sum1- max_score - min_score) / (len(scores) - 2))
8. There are two lists A and B, Use list C To get the common elements in the two lists
for example : A = [1, ‘a’, 4, 90] B = [‘a’, 8, ‘j’, 1] --> C = [1, ‘a’]
a = [12, 32, 73, 5, 83, 66, 52]
b = [82, 83, 85, 77, 87, 76, 90, 84]
c = []
for i in a:
if i in b:
c.append(i)
print(c)
9. There's a list of numbers , Get the maximum value in this list .( Be careful : Out of commission max function )
for example : nums = [19, 89, 90, 600, 1] —> 600
nums = [85, 77, 87, 76, 90, 84]
max_value = nums[0]
for item in nums[1:]:
if item > max_value:
max_value = item
print(max_value)
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 = [2, 1, 3, 2, 1, 6, 8, 5, 3, 2, 1, 5, 1, 3]
count = 0
max = []
for x in nums:
if x in max:
continue
c = nums.count(x)
if c > count:
max.clear()
count = c
max.append(x)
elif c == count:
max.append(x)
print(max)
边栏推荐
- Rhcsa - day 13
- Hlk-w801wifi connection
- Virtual machine configuration network
- 2020-03-28
- [test theory] test process management
- [Galaxy Kirin V10] [desktop] can't be started or the screen is black
- Network disk installation
- How do microservices aggregate API documents? This wave of show~
- C language structure to realize simple address book
- Latex learning insertion number - list of filled dots, bars, numbers
猜你喜欢
When I forget how to write SQL, I
From programmers to large-scale distributed architects, where are you (2)
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
Remove linked list elements
How do microservices aggregate API documents? This wave of show~
Time complexity and space complexity
Huge number (C language)
shell awk
Development guidance document of CMDB
Using SA token to solve websocket handshake authentication
随机推荐
Network disk installation
When I forget how to write SQL, I
[Galaxy Kirin V10] [server] soft RAID configuration
Rhsca day 11 operation
Work order management system OTRs
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
BGP advanced experiment
【Day1】 deep-learning-basics
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
Time complexity and space complexity
Tables in the thesis of latex learning
VLAN part of switching technology
Hlk-w801wifi connection
Basic principle of servlet and application of common API methods
Introduction to extensible system architecture
/*Rewrite the program, find the value of the element, and return the iterator 9.13: pointing to the found element. Make sure that the program works correctly when the element you are looking for does
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Student achievement management system (C language)
How to quickly parse XML documents through C (in fact, other languages also have corresponding interfaces or libraries to call)
Es entry series - 6 document relevance and sorting