如何创建以下数组

时间:2010-12-09 13:46:22

标签: python arrays

如何为列表中的项创建数组

之类的东西
list = ["a","b","c"]
  for thing in list:
    thing = [] 

5 个答案:

答案 0 :(得分:2)

我认为对你所要求的严格回应是:

for x in lst: 
    locals()[x] = []

locals可能是globals,具体取决于您的需求。但通常建议使用字典来保存这些值(正如其他人已提出的那样)。

[编辑]另一种方式:

locals().update(dict.fromkeys(lst, []))

答案 1 :(得分:1)

如果您的意思是创建全局数组(模块级别),那么您可以这样做:

for thing in list:
    globals()[thing] = []

答案 2 :(得分:0)

x = dict ([(name, []) for name in ["a", "b", "c"]])

或者

x = [[] for name in ["a", "b", "c"]]

答案 3 :(得分:0)

[list() for i in items]

[list() for i in range(len(items))]

如果要将items的当前元素包装在列表中,可以执行

[[i] for i in items]

items用于变量名称,因为list是内置的。

答案 4 :(得分:0)

list = ["a","b","c"]
for thing in list:
  exec(thing+"=[]")

这就是你想要的吗?

相关问题