Python Dict Assigning Key-Value in loop
Python Dict Assigning Key-Value in loop
I have a list of variables: [var1, var2, var3, var4].
For var1, I tried the following and it works.
d = dict(var1=True)
f = bdd.cube(d)
Now I want to loop through all variables and for each variable assign the value True.
The bdd.cube function can only take the argument d fully and not as name,value separately.
I tried to do the following, but gives me an assertion error:
AssertionError: undefined variable "var", known variables are:{var1:3, var2:11, var3:2, var4:8}
for var in variables:
d = dict(var=True):
f = bdd.cube(d)
Please let me know how to do this.
The declaration of variables are as follows:
Cvar = Enum('Cvar', 'var1 var2 var3 var 4', module=__name__)
Attributes = [CVar]
variables = List_of_Variables(Attributes)
def List_of_Variables(a):
v = w =
for attribute in a:
w = ['%s' %(i) for i in list(attribute)]
return w
If
function_F was def function_F(d): print(d), what would be the output you expect / are looking for?– jedwards
Jun 30 at 7:20
function_F
def function_F(d): print(d)
I have edited the question. I am passing the dictionary to create a bdd. So I use a standard function defined by dd library
– user3891554
Jun 30 at 7:32
Possible duplicate of How to get a variable name as a string?
– Rajat Verma
Jun 30 at 7:48
What is the use of variable
v?– Melvin Abraham
Jun 30 at 9:32
v
1 Answer
1
This is all you need:
for var in d:
d[var] = True
for loop will loop through all the keys in dictionary. The variable var is the current key (which is a string in this case). Then, we need to set that key's value to True.
for
var
True
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is the error that you are getting?
– ThePretendProgrammer
Jun 30 at 7:19