gpt4 book ai didi

Python根据列表中的信息连接数组

转载 作者:行者123 更新时间:2023-11-28 22:39:23 25 4
gpt4 key购买 nike

我有一个包含大约 500 个元素的列表。为了说明,我有:

list3 = [ 'a', 'b', 'c', 'a' ]

其中 'a'、'b'、'c' 是数组的名称:

a = np.random.normal( 0, 1, ( 500, 20 ) )
b = np.random.normal( 0, 1, ( 500, 30 ) )
c = np.random.normal( 0, 1, ( 500, 30 ) )

我想按照列表中的顺序连接列表中的数组。

因此,对于我的示例,我想获得:

C = np.concatenate( ( a, b, c, a ), 1 )

除了将数组存储在字典中然后在 for 循环中进行字符串搜索和连接之外,我不知道如何处理这个问题。有没有一种优雅的方法可以做到这一点?

最佳答案

您可以使用locals() 字典按名称访问变量

d = locals()
np.concatenate([d[x] for x in list3], 1)

关于Python根据列表中的信息连接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com