用另一个列表的元素名称创建空列表 [英] Creating empty lists with the name of the elements of another list

查看:104
本文介绍了用另一个列表的元素名称创建空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我们有一个列表为my_list=["a","b","c"].我想要做的是将空列表创建为

Say we have a list as my_list=["a","b","c"]. What I want to do is to create empty lists as

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

以便我可以根据它们的名称在其中添加一些元素.

so that I can append some elements into them according to their names.

推荐答案

在这里,它使用函数exec来执行命令,就像您键入的命令一样,但是您将i用作动态变量名

Here you go, it uses the function exec to execute the command as if it were typed by you but you use i as a dynamic variable name

for i in my_list:
  exec(i+'=[]')

请记住,这样做不是很安全

Keep in mind this isn't very safe to do

这篇关于用另一个列表的元素名称创建空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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