小编Sim*_*hal的帖子

在 Python 中使用随机函数时每次都得到零

我正在这样做,如下所示。最初我有一个二维数组 A = [(1,2,3) , (4,5,6)]。现在通过函数 func ,我想用随机数替换数组 A 两行中的所有元素。我正在尝试,但在执行该函数后,我将每个元素都设为 0。有人可以帮忙吗。请记住,我必须通过使用此函数 func 并执行这些切片操作来解决此问题。

import numpy as np
import random
A=np.array([(1,2,3),(4,5,6)])
def func(B):
    B[0:3]= np.random.random((1,3))
    return(B)
                    
for ic in range(0,2):
    A[ic,:]= func(A[ic,:])

print(A)    
Run Code Online (Sandbox Code Playgroud)
Output
Everytime I am getting zeros. There should be random numbers in both the rows of array A . I think the random number generator is generating zeros every time. Can somebody help ??
[[0 0 0]
 [0 0 0]]
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
65
查看次数

绘制半径为 R 的球体

我们怎样才能使半径为 R 的球体以给定坐标(x,y,z)为中心。就像有 10 组球心坐标和相应的 10 个不同的半径值一样。我们如何在 python 中绘制它?有什么方法可以在 python 中执行此操作,就像在 MATLAB 中一样,可以使用 surf 命令来完成此操作。在Python中我们如何实现这一点?

python matplotlib surface matplotlib-3d

4
推荐指数
1
解决办法
4563
查看次数

标签 统计

python ×2

matplotlib ×1

matplotlib-3d ×1

surface ×1