如何使用字符串变量中的列表名称 [英] how to use list name from string variable

查看:89
本文介绍了如何使用字符串变量中的列表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用列表理解来生成这两个列表.

I am generating these 2 lists using list comprehension.

lists = ['month_list', 'year_list']
for values in lists:
    print [<list comprehension computation>]

>>> ['2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005', '2004', '2003']
>>> ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

我想将这2个动态生成的列表附加到此列表名称.
例如:

I want to append these 2 dynamically generated lists to this list names.
for example :

month_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']  
year_list = ['2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005', '2004', '2003']

推荐答案

month_list = []
year_list = []
lists = [month_list, year_list]
dict = {0 : year_list, 1:month_list}

for i, values in enumerate(data[:2]):
    dict[i].append(<data>)

print 'month_list - ', month_list[0]
print 'year_list - ', year_list[0]

>>> month_list -  ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> year_list -  ['2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005', '2004', '2003']

这篇关于如何使用字符串变量中的列表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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