0

I am trying to figure out how to best create a python dictionary where the values are variable names

e.x.

ruleLst = ["gender+brand","sport+gender","sport+gender+brand"]

for i in ruleLst:
    
    new_values = i.split("+")
    rules.update({i:new_values})

rules

returns:

{
 'gender+brand': ['gender', 'brand'],
 'sport+gender': ['sport', 'gender'],
 'sport+gender+brand': ['sport', 'gender', 'brand']
}

What I try to output is:

{
 'gender+brand': [gender, brand],
 'sport+gender': [sport, gender],
 'sport+gender+brand': [sport, gender, brand]
}

Where gender, brand, sport are lists defined in the code before ruleLst is defined.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
obkmrk
  • 93
  • 1
  • 5
  • 4
    This smells like an [X-Y Problem](//meta.stackexchange.com/q/66377/174780). Please include what problem you are trying to solve here, not how to achieve one detailed bit of a perceived solution. – Pranav Hosangadi Jan 03 '22 at 17:33
  • 4
    What you show as your desired output **is not** "the values are variable names". What you show is "the values are the values of already-existing variables". There isn't a "variable-name type", so what you ask doesn't make sense. Anyway, the straightforward way to solve the problem is to *not create those variables in the first place*, but instead directly put the values in place. Failing that, at least start with a dictionary that maps `{'gender': }` etc. instead of separate variables. – Karl Knechtel Jan 03 '22 at 17:38

3 Answers3

1

You can use list comprehension like:

ruleLst = ["gender+brand","sport+gender","sport+gender+brand"]
gender = ["M", "F"]
sport = ["basket", "volleyball"]
brand = [1, 2]
a = {i: [globals()[j] for j in i.split("+")] for i in ruleLst}
print(a)

and output:

{
'gender+brand': [['M', 'F'], [1, 2]],
'sport+gender': [['basket', 'volleyball'], ['M', 'F']],
'sport+gender+brand': [['basket', 'volleyball'], ['M', 'F'], [1, 2]]
}

Reference:

kingkong
  • 122
  • 6
  • wondering if you also know how to amend the list comprehension if the `gender`,`sport`,`brand` variables wouldn't be lists but we would instead have a dictionary to pull the lists from values like: `dict = {'brand':[1,2], 'gender':['M','F'], 'sport':['football','volleyball']}`. Thanks – obkmrk Jan 04 '22 at 08:30
  • @obkmrk you can try like: `a = {i: [dict[j] for j in i.split("+")] for i in ruleLst}` – kingkong Jan 04 '22 at 14:52
  • You nailed it yet again! Thanks – obkmrk Jan 04 '22 at 17:11
0

Try using globals()[new_values[0]] and globals()[new_values[1]] instead of new_values to get the variables value.
Like this:

ruleLst = ["gender+brand","sport+gender","sport+gender+brand"]
rules = {}
gender = ["male", "female"]
sport = ["basket", "volleyball"]
brand = ["x", "y"]

for i in ruleLst:
    
    new_values = i.split("+")
    rules.update({i:[globals()[new_values[0]], globals()[new_values[1]]]})

print(rules)
Paul Kocian
  • 487
  • 4
  • 20
0

You could solve this with the map function like this. By casting it as a dict you'll get the dictonary you want.

rules = dict(map(lambda elem: [elem, elem.split('+')], ruleLst))
Jesper
  • 60
  • 5