Python 从列表中随机选择这些数字的百分比
我想随机选择这些数字的25%作为附加y。 y=?(len(x)*25%)Python 从列表中随机选择这些数字的百分比,python,arrays,numpy,Python,Arrays,Numpy,我想随机选择这些数字的25%作为附加y。 y=?(len(x)*25%) 我该怎么做?如果我理解正确,这就是您想要的: import random import numpy as np x = np.random.randint(1,101,100) x Output: array([ 87, 50, 89, 99, 17, 29, 33, 17, 29, 64, 3, 80, 93, 30, 97, 99, 69, 28, 13, 3
我该怎么做?如果我理解正确,这就是您想要的:
import random
import numpy as np
x = np.random.randint(1,101,100)
x
Output:
array([ 87, 50, 89, 99, 17, 29, 33, 17, 29, 64, 3, 80, 93,
30, 97, 99, 69, 28, 13, 32, 18, 95, 40, 75, 30, 47,
96, 67, 40, 12, 68, 69, 39, 93, 70, 73, 63, 12, 27,
91, 58, 89, 37, 2, 47, 66, 42, 72, 73, 64, 6, 21,
91, 83, 70, 69, 61, 11, 5, 46, 28, 25, 53, 10, 100,
93, 36, 66, 6, 31, 54, 95, 19, 97, 16, 80, 61, 5,
27, 29, 65, 55, 77, 71, 23, 71, 2, 95, 25, 29, 3,
78, 74, 1, 92, 95, 100, 20, 55, 75])
随机导入
将numpy作为np导入
x=np.random.randint(1101100)
_25_%=整数(len(x)/4)#100/4=25;百分之二十五
_25%列表=随机样本(列表(x),\u 25%#
np\u数组=np.array(\u 25\u百分比\u列表)#
打印(np_数组)
是的,我知道了,谢谢:)
import random
import numpy as np
x = np.random.randint(1,101,100)
_25_percent = int(len(x)/4) # 100/4 = 25; 25% of 100
_25_percent_list = random.sample(list(x), _25_percent) #<class 'list'>
np_array = np.array(_25_percent_list) #<class 'numpy.ndarray'>
print(np_array)