Python:动态创建数组对象 [英] Python: Create Array Objects Dynamically

查看:57
本文介绍了Python:动态创建数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个数组中的变量名动态创建数组.说我有:

I want to dynamically create arrays with variable names from another array. Say that I have:

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

我现在要使用Array1中提供的名称,以编程方式为Array1中的每个值创建一个新数组(其中我不知道Array1中实际有多少个值,我也不知道其中的名称).如此有效,它将给我:

I want to now programatically create a new array for each value in Array1 (where I do not know how many values will actually be in Array1 and I do not know the names that will be in there) using the names provided in Array1. So effectively it will give me:

    a = []
    b = []
    c = []

推荐答案

当您执行 Array1 = [a,b,c] 时,您会丢失有关您以前使用的变量名称的任何信息实例化数组,所以我假设您的意思是 Array1 = ['a','b','c'] .在Python中,我们通常使用 dict ionary解决与此相关的问题.使用字典,我们可以将'a'映射到一个空的 list ,如下所示:

When you do Array1 = [a,b,c], you lose any information about the names of the variables you used to instantiate the array, so I'll assume you meant Array1=['a','b','c']. In Python, we generally use a dictionary to solve issues related to this. Using a dictionary, we can have a mapping from 'a' to an empty list like so:

Array1 = ['a','b','c']
dicty = {}
for i in Array1:
    dicty[i] = []

如果这不能帮助您解决问题,请给我更多有关您要解决的问题的信息.

If this doesn't help you solve your problem, please give me more information about what problem you are trying to solve.

这篇关于Python:动态创建数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆