0

I make a bunch of matrices that I want to store in python dictionaries and I always find myself typing the same thing for every state that I want to build, i.e.

Ne21_1st_state = {}
Ne21_2nd_state = {}
Ne21_3rd_state = {}
Ne21_4th_state = {}
Ne21_5th_state = {}
Ne21_6th_state = {}
...
Ne21_29th_state = {}
Ne21_30th_state = {}

Can somebody help me automate this using python for loops?
Thanks in advance!

I want something like this:

for i in range(3, 11):
    states = f'Ar36_{i}th_state'
    print(states)

where the output would be:

Ar36_3th_state
Ar36_4th_state
Ar36_5th_state
Ar36_6th_state
Ar36_7th_state
Ar36_8th_state
Ar36_9th_state
Ar36_10th_state

but instead of printing it it would create individual dictionaries named Ar36_3th_state, Ar36_4th_state, Ar36_5th_state, ...

vlaaaaaddd
  • 19
  • 5

3 Answers3

1

can't we make a List of dictionaries

List of 30 (or any N) elements where each element is a dictionary with key = "Ar36_{i}th_state" and value = {whatever value you want}

Rajarshi Ghosh
  • 452
  • 1
  • 9
0

One way I've done this is using list comprehension.

key = list(
str(input(f"Please enter a Key for value {x + 1}: "))
if x == 0
else str(input(f"\nPlease enter a Key for value {x + 1}: "))
for x in range(3))

value = list(str(input(f"\nPlease enter a Bool for value {x + 1}: ")) 
for x in range(3))

BoolValues = dict(zip(key, value))

I first create a list of keys followed by a list of the values to be stored in the keys. Then I just zip them together into a dictionary. The conditional statements in the first list are only for a slightly better user-experience with \n being added if it's passed the first input.

Actually now that I look back on the question it may be slightly different to what I was thinking, are you trying to create new dictionaries for every matrix? If that is the case, is it something similar to this?: How do you create different variable names while in a loop?

DyingInCS
  • 51
  • 5
  • Yes. I want to create each dictionary (`Ne21_1st_state`, `Ne21_2nd_state`, `Ne21_3rd_state`,...) using a couple of lines of code without having to manually type each one from `1st` all the way to `30th` state. – vlaaaaaddd Jan 14 '22 at 05:11
0

You can create "name" of pseudo variable and use it as key in dictionary like:

my_dic = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
my_empty_dic = {}

solution = {}

for i in range(1, 31):
    name = 'Ne21_'+str(i)+'st_state'

    #solution[name] = my_dic
    solution[name] = my_empty_dic

for pseudo_variable in solution:
    print(pseudo_variable, solution[pseudo_variable])

print(solution['Ne21_16st_state'])

for pseudo_variable in solution:
    if '_16st' in pseudo_variable:
        print(pseudo_variable, solution[pseudo_variable])
  • this is close but ultimately I want to create those (empty) dictionaries (named `Ne21_1st_state`, `Ne21_2nd_state`, ...) at the end so I can attach some values to them – vlaaaaaddd Jan 14 '22 at 05:41
  • just replace my_dic by empty_dic... – Marcel Suleiman Jan 14 '22 at 05:46
  • I tried that but it's just printing the names of the dictionaries (not creating them) – vlaaaaaddd Jan 14 '22 at 05:49
  • add this line at the end "print(solution)" and run it. What you see? 1 big dictionary, fully of 30 empty dictionaries with names 1st, 2st etc. – Marcel Suleiman Jan 14 '22 at 05:54
  • True.. but I think I might have been unclear on what I really want to achieve here. Basically, I'd like a code that gives me an action equivalent to typing in `Ne21_1st_state = {}`, `Ne21_2nd_state = {}`, `Ne21_3rd_state = {}`, ... all the way to however many state I have where each one creates an empty dictionary. – vlaaaaaddd Jan 14 '22 at 06:07
  • you want Ne21_1st_state = {} ... now you have solution['Ne21_1st_state'] = {}. Your dynamic created variable is -> solution['Ne21_1st_state'] <- . If you want add something to "Ne21_1st_state" just use solution['Ne21_1st_state'] = 'value'. In this case, key in dictionary is something like variable. – Marcel Suleiman Jan 14 '22 at 06:20
  • oh shooot! I see what you're saying. It's not exactly what I was looking for but I can tweak a few things to get it to my liking. Thank you!! – vlaaaaaddd Jan 14 '22 at 06:23