如何添加、创建多个相同的项并将它们添加到一个数组中?

2024-04-29 14:16:24 发布

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

我试图创建一个shape(1,inter)[即1行,inter列]数组,其中inter是用户输入

如果你看下面的代码

l_o_s, Inter, n_o_s, L, d_o_s都来自用户输入

n_o_s表示轴总长度上的截面数,其长度对应于l_o_s中的值,直径对应于d_o_s中的值。你知道吗

所以呢

Section 1 has a length of 1.5 and diameter 3.75

Section 2 = length of 4.5-1.5 = 3 and diameter 3.5

Section 3 = length of 7.5-4.5 = 3 and diameter 3.75

等等。。。你知道吗

下面是竖井布置图: This is a shaft of length = 36, with 13 sections that have different size diameters

Inter是我在分析中需要的间隔数,在本例中Inter是3600,因此我需要一个(13600)数组。你知道吗

si是一个数组,它是l_o_s中单个节的长度、系统的总长度(L)和间隔(Inter)的函数(数学)。你知道吗

问题来了

所以如果你把所有的价值都考虑进去 si = [ 150. 450. 750. 1050. 1350. 1650. 1950. 2250. 2550. 2850. 3150. 3450. 3600.]

我需要一个形状数组(13600),它的前150个元素都等于第1节(3.75)的直径,150到450之间的元素我需要它们等于第二节(3.5)的直径,依此类推。。。你知道吗

所以我需要第一个150个元素对应于dous中的索引0,接下来的300个元素对应于dous中的索引1,以此类推。。。你知道吗

这是一个我开始的代码,但我认为它不值得谈论。我正在创建一个0数组,其内部形状对应于150300300元素中的每一个。你知道吗

import numpy as np
import math 

L = 36
Inter = 3600
n_o_s = 13
l_o_s = np.asarray([1.5,4.5,7.5,10.5,13.5,16.5,19.5,22.5,25.5,28.5,31.5,34.5,36])
d_o_s = np.asarray([3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75])
si = np.asarray((l_o_s/L)*Inter)
print(si)





z = (si.size) 
def f(x):
for i in si:
    zz = np.zeros((x,1,int(i)))
    for j in range(int(z)):
        for p in range(int(d_o_s[j])):
            zz[j][0][p] = np.full((1,int(i)),(math.pi*d_o_s**4)/64)
return zz


print(f(z))

任何想法, 达拉斯

这就是我最终得到的结果,但我只收到了3599个值,而不是所需的3600个有什么想法吗?我使用diameter输出另一个变量(基本上是用d\u o\u s中的diameters替换I\u o\u s中的值)

L = 36
Inter = 3600
n_o_s = 13
l_o_s = np.asarray([0,1.5,4.5,7.5,10.5,13.5,16.5,19.5,22.5,25.5,28.5,31.5,34.5,36])
d_o_s = np.asarray([3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75,3.5,3.75])
i_o_s = (math.pi*d_o_s**4)/64
si = np.asarray((l_o_s/L)*Inter)

lengths = si[1:] - si[:-1]

Iu = np.asarray(sum([[value]*(int(length)) for value, length in zip(i_o_s, lengths)], []))
print(Iu,Iu.shape)

Tags: ofin元素fornpsection数组length
1条回答
网友
1楼 · 发布于 2024-04-29 14:16:24

在python中,像4 *[1]这样的操作产生[1,1,1,1]。因此,您需要计算子阵列的长度,创建它们,并使用sum()连接它们。你知道吗

lengths = si[1:] - si[:-1]

result = sum([
    [value]*length for value, length in zip(d_o_s, lengths)
], [])

另外,您的si数组是float类型的,因此在用作索引时会出现舍入错误。通过改变

si = np.asarray((l_o_s/L)*Inter)

si = np.asarray((l_o_s/L)*Inter).astype(int)

相关问题 更多 >