Home > OS >  Generating empty lists with names from items on a differnt list
Generating empty lists with names from items on a differnt list

Time:03-15

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. But I also need to do computations combining all 'A', 'B' and 'C'(like mean and ratios).

CodePudding user response:

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().

  • Related