import numpy as np
arr=np.array([[20,-33,40,-46,56],[13,25,-17,98,-20]])
sum_arr=(arr>0).sum()
print(sum_arr)
6
import numpy as np
arr=np.array([[20,-33,40,-46,56],[13,25,-17,98,-20]])
sum_arr=arr[arr>0].sum()
print(sum_arr)
sum_assert = 20 + 40 + 56 + 13 +25 +98
print(sum_assert)
252
252
import numpy as np
arr=np.array([[20,-33,40,-46,56],[13,25,-17,98,-20]])
arr_bool=arr>0
any_bool=arr_bool.any()
all_bool=arr_bool.all()
print(any_bool)
print("-----------------------------")
print(all_bool)
True
-----------------------------
False
red_arr1=[20,30,40,50,60,70,80,90]
red_arr2=[0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
bool_arr=[True,False,True,False,True,False,False,True]
red_result=[(red1 if flag else red2)for red1,red2,flag in
zip(red_arr1,red_arr2,bool_arr)]
print(red_result)
[20, 0.3, 40, 0.5, 60, 0.7, 0.8, 90]
import numpy as np
red_arr1 = np.array([20, 30, 40, 50, 60, 70, 80, 90])
red_arr2 = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
bool_arr_a = np.array([True, False, True, False, True, False, False, True])
bool_arr_b = np.array([False, True, False, True, True, False, True, True])
both_true = bool_arr_a & bool_arr_b
print(both_true)
either_true = bool_arr_a | bool_arr_b
print(either_true)
sum_arr = red_arr1 + red_arr2
half_sum_arr = sum_arr / 2
red_result = np.where(both_true, 0, np.where(either_true, half_sum_arr, sum_arr))
print(red_result)
[False False False False True False False True]
[ True True True True True False True True]
[10.1 15.15 20.2 25.25 0. 70.7 40.4 0. ]
import numpy as np
man=np.array([[20,21,23],[25,26,27]])
woman=np.array([[23,22,20],[27,28,26]])
all=np.concatenate([man,woman],axis=0)
cope=np.concatenate([man,woman],axis=1)
print(all)
print("------------------")
print(cope)
[[20 21 23]
[25 26 27]
[23 22 20]
[27 28 26]]
------------------
[[20 21 23 23 22 20]
[25 26 27 27 28 26]]
import numpy as np
cope=np.array([20,21,23,23,22,20,25,26,27,27,28,26])
split_cope=np.split(cope,[1,3,6,7])
print(split_cope)
[array([20]), array([21, 23]), array([23, 22, 20]), array([25]), array([26, 27, 27, 28, 26])]
import numpy as np
cope=np.array([20,21,23,23,22,20,25,26,27,27,28,26])
cope.sort()
print(cope)
[20 20 21 22 23 23 25 26 26 27 27 28]
import numpy as np
cope=np.array([[20,23,21,22,24],[28,25,27,26,29]])
cope.sort(axis=1)
print(cope)
[[20 21 22 23 24]
[25 26 27 28 29]]
import numpy as np
names=np.array(["经理","副经理","主管","主管","主管","技术员","业务员"])
uni_names=np.unique(names)
print(uni_names)
['业务员' '主管' '副经理' '技术员' '经理']