开发者

How can I create the following arrays

开发者 https://www.devze.com 2023-01-29 10:58 出处:网络
How could I create arrays for items in a list someth开发者_如何转开发ing like list = [\"a\",\"b\",\"c\"]

How could I create arrays for items in a list someth开发者_如何转开发ing like

list = ["a","b","c"]
  for thing in list:
    thing = [] 


I think the strict response to what you are asking is:

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

locals may be globals, depending on what you want. But it's usually advisable to use a dictionary to hold these values instead (as other have already proposed).

[Edit] another way:

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


If you mean creating global arrays (module level) then you can do this:

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


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

?

or maybe

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


[list() for i in items]

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

If you want to wrap the current elements of items in lists, you can do

[[i] for i in items]

items is used for the variable name since list is a builtin.


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

is this what you want?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号