简体   繁体   English

从不同列表中的项目生成具有名称的空列表

[英]Generating empty lists with names from items on a differnt list

I want to create empty lists with partially named from items from different list.我想创建空列表,其中部分命名来自不同列表的项目。

items = ['A', 'B', 'C']
week_0_A =[]
week_0_B =[]
week_0_C =[] 

week_1_A =[]
week_1_B =[]
week_1_C =[]

......

What I have done till now is:到目前为止我所做的是:

week_0 =[]
week_1 =[]
week_2 =[] 
......

and did the necessary calculations for each of 'A', 'B' or 'C' separately.并分别对“A”、“B”或“C”进行必要的计算。 But I also need to do computations combining all 'A', 'B' and 'C'(like mean and ratios).但我还需要结合所有“A”、“B”和“C”(如均值和比率)进行计算。

Here's one approach using dictionaries.这是使用字典的一种方法。 You have a list of letters and number of weeks as input and you generate the dictionary named after them:您有一个字母列表和周数作为输入,并生成以它们命名的字典:

items = ['A', 'B', 'C']
weeks_count = 2

partially_named = {}
for week in range(weeks_count):
    for item in items:
        partially_named['week_{}_{}'.format(week, item)] = []
print(partially_named)
# print result: {'week_0_A': [], 'week_0_B': [], 'week_0_C': [], 'week_1_A': [], 'week_1_B': [], 'week_1_C': []}

# and you can access the lists using the names like
partially_named['week_0_A']

And if you want to get only the names, you can use partially_named.keys() .如果您只想获取名称,则可以使用partially_named.keys()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM