Python learning list [easy to understand, code can be run directly, highly recommended]

Posted by evilren on Fri, 15 Oct 2021 07:35:20 +0200

List of Python societies [easy to understand, highly recommended]

The following is the main content of this article:
'''
list
Relationship between combined data type and basic data type
Basic data type:
Value type: int,float,bool
character string
Composite data types contain many basic data types
'''
The following is the python code, including creating a list, viewing the list type, and emptying the string
section:
Slicing and indexing operations
section
Syntax format: list[start πŸ”š step]
Start: start from 0 and can be omitted (start = 0)
End: the end, but excluding the position, cannot be omitted
Step: step size

Add, delete, modify and query the list:
Add, delete, modify and query
(1) Add operation
function
append: appends an element to the end of the list
extend: merge another list (extended list) at the end of the specified list
insert: inserts an element at an index position in the list
Stack memory: fast and unstable, used to store variables
Heap memory: relatively stable. The data is stored in heap memory
#extend() function
'''
Format: list1.extend(list2)
Complexity: O(n)
The return value is None
Directly change list1, which is the result of the addition of the two lists
extend() function, the return value is None, but it will directly change the value of the list
'''
'''
inser(index,object) function to insert elements at the index position specified in the list
If the inserted element is at the end, it is the append() function
index specifies the insertion location
obj: inserted content
'''
Delete operation
pop: remove the last element in the list
remove: the first matching item in one place, with no return value
del: delete the entire list
Clear: clear the list

Sort operation:
Syntax format: list.sort(reverse = False/True)
reverse = False is in descending order
reverse = True is an ascending sort
The return value is None
'''
Sort is the method of sorting lists: you can only sort lists; No return value, the function is the original list
sorted() is used to sort all iteratable objects. It has a return value and is used to generate a new list
'''
'''
List derivation
Syntax: [temporary variable for temporary variable in iterable]
Traverse the temporary variable from the iteratable object and put it in the list
'''
#List derivation with conditional judgment
#Syntax: [x for x in iterable if condition]
#Generate an even sequence 1-100
Order one πŸ‘ Come on, autumn pear paste!!!
The code is as follows:

'''
list
 Relationship between combined data type and basic data type
 Basic data type:
Value type: int,float,bool
 character string
 Composite data types contain many basic data types
'''
#1. Declaration (creation) and assignment
#Use [] to create a list
num_list = [1,2,3,4]#int type list
name_list = ['to','stupid','stupid']#String list
#The type() function views the list type
print(type(num_list))#<class 'list'>
print(type(name_list))#<class 'list'>
print('==='*20)
#Create a list using list
num_list2 = list((1,2,3,4))
num_list3 = list()
print(num_list2)#[1,2,3,4]
print(num_list3)#[]
#There are two ways to create an empty string
lsit1 = []#common
list2 = list()
#Lists in python can store various other types of data
list3 = [1,'234','abc',[1,2,3,4]]
print(list3)
print('==='*20)
'''
Slicing and indexing operations
 section
 Syntax format: list[start:end:step]
start:Starting from 0, it can be omitted( start = 0)
end:The end, but excluding the position, cannot be omitted
step:step
'''
num1_list = [1,2,3,4,5,6,7,8,9,10]
print(num1_list[::1])#Take from 0 (0 at position is 1) to the end in steps of 1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(num1_list[1::1])#Take from 1 to the end [2, 3, 4, 5, 6, 7, 8, 9, 10]
print(num1_list[2:-1:1])#-1 corresponds to the position of 10 in the list, which is taken from 3 to 10, but 10 cannot be taken [3, 4, 5, 6, 7, 8, 9]
print(num1_list[:10:1])#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(num1_list[::2])#Starting from 0 in steps of 2 [1, 3, 5, 7, 9]
'''
List operation
 Add, delete, modify and query
(1)Add operation
 function
append:Append elements to the end of the list
extend:Merge another list (extended list) at the end of the specified list
insert:Inserts an element at an index position in the list 
Stack memory: fast and unstable, used to store variables
 Heap memory: relatively stable. The data is stored in heap memory
'''
#The append() function appends an element to the end of the list
#The operation time complexity is O(1) -- very efficient
name1_list = []#Empty list
name1_list.append('Yu Lei')
name1_list.append('YYX')
name1_list.append('zhengyuan')
name1_list.append('Anine')
print(name1_list)
# print(res)
print('==='*20)
# extend() function
'''
 Format: list1.extend(list2)
 Complexity: O(n)
 The return value is:None
 Direct change list1,list1 Is the result of adding two lists
 extend()Function, the return value is None However, it will directly change the value of the list
'''
num_list1 = [1,2,3,4]
print(id(num_list1))#id() function, a built-in function in python, is used to return the unique identification of the object. It is an integer
num_list2 = [4,3,2,1]
print(id(num_list2))
res = num_list1.extend(num_list2)
print(res)#None
print(num_list1)#[1, 2, 3, 4, 4, 3, 2, 1]
print('==='*20)
'''
inser(index,object)Function, in list Insert element at specified index position
 If the inserted element is at the end, it is append()function
index Specify the insertion location
obj: Inserted content
'''
num_list4 = [1,2,3,4]
num_list4.insert(0,0)
print(num_list4)
num_list4.insert(8,99)#Insert elements in order
print(num_list4)
print('==='*20)
'''
Delete operation
pop:Removes the last element in the list
remove:The first match in one place has no return value
del:Delete entire list
clear:clear list 
'''
num_list5 = [1,2,3,4,5,6]
#pop(index) function. Index is the subscript that specifies to delete the element. The default value is - 1 (the last element in the list)
#The return value of the pop() function is the deleted element
print(num_list5)#[1, 2, 3, 4, 5, 6]
print(num_list5.pop())#6
print(num_list5.pop(2))#3
print(num_list5)#[1, 2, 4, 5]
print('==='*20)
#remove (obj) function: deletes the contents of the specified obj in the list
#Only one element can be deleted at a time, and there is no return value
num_list6 = [1,2,3,4,5,6]
res = num_list6.remove(5)#No return value
print(num_list6)
print(res)
# num_list6.remove(9)#Error x not in list. The deleted value is not in the list
print('==='*20)
'''
del()Functions: deleting list
 Syntax format: del variable
'''
print(num_list6)
del num_list6
# print(num_list6)#Error: name 'num_list6' is not defined
print('==='*20)
'''
clear():clear list 
Syntax format: num_list6.clear()
'''
num_list5.clear()
print(num_list5)#[]
print('==='*20)
'''
Modify operation
num_lsit
(1)list.reverse()
(2)lsit.sort()Sort operation
'''
#Reverse operation: list.reverse(), without return value, reverses the original list
num_list7 = [1,2,3,4,5,6]
num_list7.reverse()
print(num_list7)#[6, 5, 4, 3, 2, 1]

#The for loop traverses the list in reverse order
for i in num_list7:
    print(i)
#Invert the list with slices
print(num_list7[::-1])#[1, 2, 3, 4, 5, 6]
print(num_list7)#[6, 5, 4, 3, 2, 1]
#Slicing does not change the structure of the list relative to reverse
print('==='*20)
'''
Sort operation:
Syntax format:list.sort(reverse = False/True)
reverse = False Is in descending order
reverse = True Is in ascending order
 The return value is None
'''
num_list8 = [11,15,3,41,2,1]
print(num_list8)#[11, 15, 3, 41, 2, 1]
num_list8.sort()#The default is ascending
print(num_list8)#[1, 2, 3, 11, 15, 41]
num_list8.sort(reverse=True)#Descending order
print(num_list8)#[41, 15, 11, 3, 2, 1]
num_list8.sort(reverse=False)#Ascending order
print(num_list8)#[1, 2, 3, 11, 15, 41]
print('==='*20)
'''
sorted()function
 Syntax format: sorted(iterarable(Iteratable object))---There is a return value
print(sorted(num_list))
sorted(iterarable(Iteratable object),reverse = )
True Is in descending order
False Is in ascending order
'''
'''
sort Yes list Method: only the list can be sorted; there is no return value and the original list is used
sorted()It is used to sort all iteratable objects. It has a return value and is used to generate a new list
'''
# Iteratable object
#character string
print(sorted('5981247',reverse=False))
#tuple
print(sorted((5,6,3,1,),reverse=True))
#aggregate
print(sorted({5,9,6,1,3,2}))

#The sorting of strings compares the ASCII code of the first letter in the string and compares the dictionary order
names = ['Anine','Ajiu','booming']
print(sorted(names,reverse=True))#Reverse order

#Modify the value of the list, directly correspond to the index subscript of the list, and assign a value
names[1] = 'Yu Benben'
print(names)#Second value replacement
namess = ['Anine','Ajiu','Annie','booming','Anine']#ASCII code of Chinese characters is smaller than English letters
print(sorted(namess))

#Query operation
print(namess.count('Anine'))#2
#index(): query the index index index of a content
print(namess.index('Annie'))#2

#List derivation -- key points
#establish
num_list9 = []
#Put an even number between 1 and 100 in the list
for i in range(2,101,2):
    num_list9.append(i)
print(num_list9)
print('==='*20)
'''
List derivation
 Syntax:[Temporary variable for Temporary variable in iterable]
Traverse the temporary variable from the iteratable object and put it into the list in
'''
num_list10 = [x for x in range(2,101,2)]
print(num_list10)

num_list11 = [[1,2,3,4],[4,5,6,4],[7,8,9,8]]#2D list
print(len(num_list11))#The size of a two-dimensional matrix is the number of one bit matrices in the matrix
#(1) Take out [1,4,7] and put it in the new list
nums = []
for i in range(len(num_list11)):
    nums.append(num_list11[i][0]) #If the column is determined and the row is uncertain, take out all the elements in the first column
print(nums)
print('--'*10)
nums = [num_list11[i][0] for i in range(len(num_list11))]#Put the for loop content of each layer into []
#         The obtained value is stuffed into the list, and the iteratable temporary variable is stuffed into the list, which is equivalent to putting the value of traversed I into the previous [i][0]
print(nums)
#Split the contents of the for loop and put them into the list derivation
#Remove [1,5,9]
#for loop
nums1 = []
for i in range(len(num_list11)):
    nums1.append(num_list11[i][i])
print(nums1)
nums1.clear()
#Obtain by list derivation [1,5,9]
nums1 = [num_list11[i][i] for i in  range(len(num_list11))]
print(nums1)
nums.clear()
#Take out all the numbers in the two-dimensional array and use the double for loop
for i in range(len(num_list11)):
    for j in range(len(num_list11)):
        nums.append(num_list11[i][j])
print(nums)
nums.clear()
#The results are obtained by list derivation
nums = [num_list11[i][j] for i in range(len(num_list11)) for j in range(len(num_list11))]
print(nums)
nums.clear()
#List derivation with conditional judgment
#Syntax: [x for x in iterable if condition]
#Generate an even sequence 1-100
for i in range(1,101):
    if i % 2 == 0:
        nums.append(i)
print(nums)
nums.clear()
#List expression to complete the above results
nums = [i for i in range(1,101) if i%2==0]
print(nums)
nums.clear()
nums = [num_list11[i][j] for i in range(len(num_list11)) if i%10000==0 for j in range(len(num_list11))]
print(nums)

Order one πŸ‘ Come on, autumn pear paste!!!

Topics: Python pygame Flask