1、 String manipulation
str1 = "asdasdasdasdasdasdasd"
print(str1[::]) And str1[:] Equivalent
# str1[ Start subscript : End subscript ( It doesn't contain )], The starting subscript defaults to 0, The end subscript defaults to the last position +1
print(str1[::3]) #3 It's the step length
print(str1[::-1])# Output in reverse order
print(x)
Slicing of multidimensional arrays
p Is a multi-dimensional array ,p.shape = (3,4,5,6)
p[a0:b0:c0,a1:b1:c1,a2:b2:c2,a3:b3:c3]
a0:b0:c0 Said to p.shape[0] This dimension operates , from a0 To b0( It doesn't contain ), In steps of c0
2、 String output
The longest way to use it is format Format output , adopt .format Come to str Medium {
} Fill in the content .
The second way is %s
such as :
str2 = " my %s It's the strongest ".
print(str2 % 'Coding') # You can put % The back Coding Replace with %s
Print the results : my Coding It's the strongest .
3、 About three strings
""" aaa """
If there are variables to accept , that aaa It's just a normal string , If not, then aaa It's just a comment .
4、list
list1 = []
list1.append(1)
list1.insert(1,2)# Insert... In a fixed position
print(list1)
list1.extend([3,4,5])# Will list [3,4,5] Add elements from to lsit1 after
list2 = [3,4,5]+list1# List splicing
list2[-1] = "asdad"# As modified
# The list is deleted by element : Pay attention to execute once remove Only one element can be deleted , The same elements , Delete elements with small subscripts .
list2.remove(list2[-1])
list Sort
list.sort(list1)
5、tuple
tuple A tuple type , Cannot change element , Support list The index of , section
6、set
set aggregate , disorder , Cannot index slice , And {
} Easy to confuse .
x = set([1,1,2,3,4,234,0])
y = {
}# The definition is a dictionary
# print(x[0]) # Report errors
Pop up the first element , Delete and return the first element
cnt = x.pop()
7、dict
{
key:value},key only , identical key Will be covered
Definition
dic = {
} perhaps dic = dict()
Can press key Indexes
print(dic[key])# Go back to the dictionary key Corresponding value
Dictionary delete operation
del x[key]
del x Delete entire dictionary , Reclaiming memory
x.clear() Empty dictionary , Reclaiming memory
Get all the... In the dictionary keys
print(list(x.keys()))# Returns all of the dictionary in the form of a list key value
print(list(x.values()))# Returns all of the dictionary in the form of a list value value
Get all of the dictionary key:value
y = {
1:2,3:4}
x = list(y.items())
print(x)
Output :[(1,2),(3,4)]
This method can be used to traverse the key value pairs of the dictionary
8、 Logical operations ( And C++ A little difference between )
and or not (&& || !)
9、 Split string
str.split() For segmentation string, The default is to split with spaces , After splitting, return a list
You can customize the split method
str.split("!") Indicates split with exclamation point
10、 Remove string Space on both sides
# print(str2.strip())
# print(str2)
# Used to remove the space on the left
# str.lstrip()
# Used to delete the blank character on the right
# str.rstrip()
11、 Replace
# Replace
# str3 = "asda adasd dsfsd"
# Is to replace the space with !
# print(str3.replace(" ", "!"))
12、chr
chr(X) If X It's a int Type number ,chr You can return to X by ASCII Code characters
It corresponds to ord function
print(ord("a"))# Get is "a" Corresponding ASCII code = 97
13、map function
map(Function,iterable)
iterable Act on Function After the return
for example :
x = map(int , "123")
print(list(x))
[1,2,3]
x = map(lambda x,y : x + y,[1,2,3],[4,5,6])
x The value of is the first list = [1,2,3],y The values for list = [4,5,6]
print(list(x))
14、python Class
python Medium pass and return
pass To do nothing , It is set for syntax specification to prevent error reporting .
return Exit the current function , Often used in if else In structure
class Net([ inheritable ]):
def __init__(self,x1,x2):
self.x = 1
...
pass
def F1(self):
....
return
#net For the class Net An example of ( object )
net = Net(x1 = X1,x2 = X2)
15、OS
import os
os.mkdir(path) # stay path Create folder under path , If the folder already has a value, an error is reported .
os.path.exists(path)# return True/False, Judge the path path Does the file under exist
os.path.join(A,B,C,...)# Splicing file path
os.path.abspath(file) # return file File path
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"# solve jupyter Problems that hang up at runtime .