如何将列表转换为数组?

发布于 2025-01-16 04:05:55 字数 631 浏览 0 评论 0原文

我有一个列表和一个(2×2)数组。

我想将列表的前两个值与下面所示的数组“k”相乘,

(当然是将这两个值转换为(2 x 1)数组)。

然后我想对接下来的两个值(即第二个和第三个值)执行相同的过程。

就像 [1,2] ,然后 [2,3] ...[3,4] [4,5] ...( 这些每对列表必须位于 2 x 1 数组中,以便我可以进一步将其相乘与下面提到的k(2×2数组))

所有情况下的数组“k”都是相同的。

如何制作这个循环?

my_List = [1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13,14, 15, 16, 17 ,18 ,19, 20, 21, 22, 23, 24, 25, 26,27 ,28 ,29, 30, 31 ,32, 33, 34,35 ,36, 37, 38, 39]
E = 1
I = 1
l = 1

k = np.array(  [  [4*E*I/ l,       2*E*I/ l  ],
                  [2*E*I/ l,       4*E*I/ l  ] ] )

I have a list and a (2 by 2) array.

I want to multiply the first two values of the list with this array 'k' shown below,

(of course by converting these two values into a (2 by 1) array).

Then I want to do the same procedure with the next two values( i.e 2nd and 3rd values).

its like [1,2] , then [2,3] ...[3,4] [4,5] ...( these each couple of lists must be in 2 by 1 array so that I can further multiply it with k (2 by 2 array) mentioned below )

The array 'k' in all the cases is the same.

How do I make this loop?

my_List = [1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13,14, 15, 16, 17 ,18 ,19, 20, 21, 22, 23, 24, 25, 26,27 ,28 ,29, 30, 31 ,32, 33, 34,35 ,36, 37, 38, 39]
E = 1
I = 1
l = 1

k = np.array(  [  [4*E*I/ l,       2*E*I/ l  ],
                  [2*E*I/ l,       4*E*I/ l  ] ] )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-01-23 04:05:55

这是您正在寻找的解决方案吗?如果没有添加一些预期的输出,以便可以进行相应的编辑。

import numpy as np

# Turn my_List into list of (2 by 1) arrays
twosArray = [np.array([my_List[i], my_List[i+1]]) for i in range(0, len(my_List)-1, 2]

# Multiply all (2 by 1) arrays by k
for array in twosArray:
    prod = k.dot(array) # you can collect prod into a list if you want to use it later
    print(prod)

测试:

# I'm using simpler values for my_List. You can test with your own values
my_List = [2,2,-1,-1]
# k is the same as the one in your problem

打印输出:

[12. 12.]

[-6. -6.]

Is this the solution you are looking for? If not add some expected output so that this can be edited accordingly.

import numpy as np

# Turn my_List into list of (2 by 1) arrays
twosArray = [np.array([my_List[i], my_List[i+1]]) for i in range(0, len(my_List)-1, 2]

# Multiply all (2 by 1) arrays by k
for array in twosArray:
    prod = k.dot(array) # you can collect prod into a list if you want to use it later
    print(prod)

Test:

# I'm using simpler values for my_List. You can test with your own values
my_List = [2,2,-1,-1]
# k is the same as the one in your problem

Outputs of print:

[12. 12.]

[-6. -6.]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文