Python函数和数组参数

2024-10-05 14:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个程序,可以产生(x,y,z)10x10x10阵列(单位细胞)上的三维网格。示例:一个立方体位于点(0,10)x(0,10)x(0,10),而另一个立方体位于点(0,10)x(10,20)x(0,10)。到目前为止,我有一个函数,使其中4个,但我如何将它自动化,使几十个,甚至数百个?你知道吗

import math
import numpy as np


def cubeplot():
    count = 0
    count2 = 0
    x = 11
    y = 11
    z = 11
    c = 11
    parameter = np.arange(0,11,1)
    xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
    valuesrange = np.zeros((11, 11, 11))
    parameter2 = np.arange(c, y+10,1)
    xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
    valuesrange2 = np.zeros((x+10, y+10 , z + 10))
    parameter3 = np.arange(c + 10, y+20,1)
    xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
    valuesrange2 = np.zeros((x+20, y+20 , z +20))

    print('POINT 1')
    while (count < 1):
        xint = np.random.randint(0,2)
        yint = np.random.randint(0,2)
        zint = np.random.randint(0,2)

        if xint > 0:
            xint = np.random.randint(10,c, 22)
            x = 10
        else:
            xint = np.random.randint(0,1, 22)
            x = 0
        if yint >0:
            yint = np.random.randint(10,c, 22)  
            y = 10
        else: 
            yint = np.random.randint(0,1, 22)
            y = 0
        if zint > 0:
            zint = np.random.randint(10,c, 22)  
            z = 10
        else:
            zint = np.random.randint(0,1, 22)
            z = 0
        count = count + 1
        print('x = ' + str(x))
        print('y = ' + str(y))
        print('z = ' + str(z))
    #       print('Distance = ' + str(zint))

    print('POINT 2')

    while (count2 < 1):
        xint = np.random.randint(0,2)
        yint = np.random.randint(0,2)
        zint = np.random.randint(0,2)

        if xint > 0:
            xint = np.random.randint(20,c + 10, 22)  
            x2 = 20
        else:
            xint = np.random.randint(10,11, 22)
            x2 = 10
        if yint >0:
            yint = np.random.randint(10,c, 22)  
            y2 = 10
        else: 
            yint = np.random.randint(0,1, 22)
            y2 = 0
        if zint > 0:
            zint = np.random.randint(10,c, 22)  
            z2 = 10
        else:
            zint = np.random.randint(0,1, 22)
            z2 = 0
        count2 = count2 + 1
        print('x = ' + str(x2))
        print('y = ' + str(y2))
        print('z = ' + str(z2))

    distance = ((x2-x)**2 + (y2 - y)**2 + (z2 - z)**2)**.5   
    print ('POINT 1: x,y,z: ' + str(x) +',' + str(y) + ','+ str(z))
    print ('POINT 2: x,y,z: ' + str(x2) +',' + str(y2)+ ',' + str(z2))
    print('Distance = ' + str(distance))

    print('POINT 3')
    count3 = 0
    while (count3 < 1):
        xint = np.random.randint(0,2)
        yint = np.random.randint(0,2)
        zint = np.random.randint(0,2)

        if xint > 0:
           xint = np.random.randint(10,c, 22)  
           x3 = 10
        else:
            xint = np.random.randint(0,1, 22)
            x3 = 0
        if yint >0:
            yint = np.random.randint(10,c, 22)  
            y3 = 10
        else: 
            yint = np.random.randint(0,1, 22)
            y3 = 0
        if zint > 0:
            zint = np.random.randint(20,c + 10, 22)  
            z3 = 20
        else:
            zint = np.random.randint(10,c, 22)
            z3 = 10
        count3 = count3 + 1
        print('x = ' + str(x3))
        print('y = ' + str(y3))
        print('z = ' + str(z3))

    print('POINT 4')
    count4 = 0
    while (count4 < 1):
        xint = np.random.randint(0,2)
        yint = np.random.randint(0,2)
        zint = np.random.randint(0,2)

        if xint > 0:
            xint = np.random.randint(20,c+10, 22)  
            x4 = 20
        else:
            xint = np.random.randint(10,c, 22)
            x4 = 10
        if yint >0:
            yint = np.random.randint(10,c, 22)  
            y4 = 10
        else: 
            yint = np.random.randint(0,1, 22)
            y4 = 0
        if zint > 0:
            zint = np.random.randint(20,c + 10, 22)  
            z4 = 20
        else:
            zint = np.random.randint(10,c, 22)
            z4 = 10
        count4 = count4 + 1
        print('x = ' + str(x4))
        print('y = ' + str(y4))
        print('z = ' + str(z4))
        print ('POINT 3: x,y,z: ' + str(x3) +',' + str(y3) + ','+ str(z3))
        print ('POINT 4: x,y,z: ' + str(x4) +',' + str(y4)+ ',' + str(z4))
        print('SET END')
        print('')
        print('')
        print('')


runtime = int (input("How many times would you like to run the program?: "))
maincount = 0
print ('The program will run', runtime, 'times')

while (maincount < runtime):
    cubeplot()
    maincount = maincount + 1

Tags: ifnprandomelsepointprintx2randint
1条回答
网友
1楼 · 发布于 2024-10-05 14:31:09

numpy arrays比你想象的要强大一些。如果您希望三维网格的每个单元都有另一个三维网格,那么您需要的是一个6D网格。检查以下示例:

import numpy as np

# Lets make a 3D grid in which each cell has 3 dimensions
# (making it a 6D grid)
grid = np.zeros((3, 2, 4, 10, 10, 10))

# So for every arbitrary 3D coordinate of the grid I have
# another 3D grid (result: (10, 10, 10))
print(grid[2, 1, 3].shape)

# If a make a grid for a single cell:
# the randint function is creating a (10, 10, 10) grid
local_grid = np.random.randint(0, 10, (10, 10, 10))

# I can put it inside my big grid:
grid[2, 1, 3] = local_grid

# If I need to this for all (3, 2, 4) shape of my big grid
# (meaning its first 3 dimensions) I can loop each of those
# dimensions:
for x in range(grid.shape[0]):
    for y in range(grid.shape[1]):
        for z in range(grid.shape[2]):
            grid[x, y, z] = np.random.randint(0, 10, (10, 10, 10))

根据您的需要,还有其他填充网格的方法(可能更为优化),但是您几乎不需要使用指数化数字,除非您确实需要这样做。这就是你要找的(逻辑上)?你知道吗

编辑:因此,如果您真的只需要一个三维网格,您可以将单元格尺寸乘以网格尺寸,然后循环索引其中的一些内容。例如:

target = (3, 2, 4)
cell = (3, 3, 3)
grid = np.zeros((target[0]*cell[0], target[1]*cell[1], target[2]*cell[2]))
counter = 1
for x in range(target[0]):
    for y in range(target[1]):
        for z in range(target[2]):
            grid[x*cell[0]:x*cell[0]+cell[0], y*cell[0]:y*cell[1]+cell[1], z*cell[2]:z*cell[2]+cell[2]] = counter
            counter += 1

在这种情况下,grid[0:cell[0], 0:cell[1], 0:cell[2]]将等于cell维度的网格,其内容是1。你知道吗

相关问题 更多 >