python生成随机数组

Python013

python生成随机数组,第1张

从已有数组中提取随机数组

要求:从两个不同数组中随机抽取数组,用到函数np.random.choice

import numpy as np

hyper=[1,2,5,8,9,12,13,14,17,19]

noh=[3,4,6,7,10,11,15,16,18,20]

#h:n 2:2

l1=np.random.choice(hyper,2,replace=False)

l2=np.random.choice(noh,2,replace=False)

ll=[l2[0],l1[0],l1[1],l2[1]]

print(ll)

l1=np.random.choice(hyper,2,replace=False)

l2=np.random.choice(noh,2,replace=False)

ll=[l1[0],l2[0],l1[1],l2[1]]

print(ll)

l1=np.random.choice(hyper,2,replace=False)

l2=np.random.choice(noh,2,replace=False)

ll=[l1[0],l1[1],l2[0],l2[1]]

print(ll)

l1=np.random.choice(hyper,2,replace=False)

l2=np.random.choice(noh,2,replace=False)

ll=[l2[1],l2[0],l1[0],l1[1]]

print(ll)

from numpy import *

def creat_array_0(n, m):

# 生成一个n*m的零矩阵

matrixA = [None] * n

for i in range(n):

newaxis =[]

for j in range(m):

newaxis.append(random.randint(1,101))

matrixA[i] = newaxis

matrixB = array(matrixA)

return matrixB

if __name__ == '__main__':

print(creat_array_0(8,5))

from random import randint

ar=[[randint(1,100) for _ in range(4)] for _ in range(5)]

print(ar)