Python: common function query

Function name: enumerate()
function description: show both the subscript and index values

num = np.array([1,3,5,7,9])
for index,value in enumerate(num):
    print(index,value)

result

0 1
1 3
2 5
3 7
4 9

Function name: lstrip (), rstrip ()
function description: delete the beginning (end) of the specified string

str = "888888 love 999999"
str1 = str.lstrip("8")
str2 = str1.rstrip("9")
print(str1)
print(str2)

result

 love 999999
 love 

Function name: replace()
Function description: The method replaces old (old string) with new (new string) in the string. If the third parameter max is specified, the replacement will not exceed max times

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

result

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Function name: rfind()
Function description: Returns the position of the last occurrence of the string (query from right to left), or -1 if there is no match.

str = "888888 love 999999"
index1 = str.rfind("8")
index2 = str.rfind("6")
print(index1)
print(index2)

result

5
-1

Function name: rjust()
Function description: Returns a new string with the original string right-aligned and padded with spaces to the length width. If the specified length is less than the length of the string, the original string is returned.

str = "888888 love 999999"
str1 = str.rjust(30,'1')
print(str1)

result

111111111111888888 love 999999

Function name: random.sample()
function description: select numbers that are not repeated from the sample

num = [1,2,3,4,5,6,7,8,9,10]
rand_num = random.sample(num,5)
print(rand_num)

result

[10, 2, 7, 4, 1]

Function name: mean(),sum()
function description: returns the average sum of the array

num = np.array([[1,2,3],[4,5,6],[7,8,9]])
#按照行压缩求均值
print(np.mean(num,axis = 0))
#按照列压缩求均值
print(np.mean(num,axis = 1))

result

[4. 5. 6.]
[2. 5. 8.]

Function name: where()
Function description: return the subscript that meets the condition, or change the value

num = np.arange(1,10)
#返回下标
print(np.where(num>5))
#满足条件的赋值
num1 = np.where(num%2==0,num,0)
print(num1)

result

(array([5, 6, 7, 8], dtype=int64),)
[0 2 0 4 0 6 0 8 0]

Function name: choice
function description: randomly choose from the choices provided

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')

result

choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n

Guess you like

Origin blog.csdn.net/kiwi_berrys/article/details/103962470