0
votes

I am trying to process a dictionary with keys and each key having a list of values and create a nested dictionary from it. And also compare the values for equality in the list for each key and generate a new dictionary with the list of equal and unequal values.

I was able to loop through the list of values for each key and process them to get a new variable with which I wanted to create the new nested dictionary. But currently a null exception is thrown at the first key value.

dict1 = {a:[d,e,f], b:[p,q,r]}
dict2 = {d:100, e:100, f:100, p:100, q:100, r:100}
dict3 = {d:text1, e: text2, f: text3}

for i in dict1.keys():
    for x in dict1[i]:
        if dict2[x] == 100:
            string = re.findall(r'sometext in text',dict3[x])[0]
            ver = re.search('(?is)<i>(.+?)</i>', match_string).group(1)
            d[i][x] = ver

Expected result:

d = { a:{d:ver1, e:ver2, f:ver3}, b:{p:ver4, q:ver5, r:ver6 }

After this looping through each nested value, each value needs to be compared with its peers value and arranged in a new dictionary with the keys of the values matching. Something as below:

if d's ver1 = e's ver2 =! f's ver3
    dict4 = {a: { equal:[d,e], unequal: [f]}
1
That is not a valid dict, d is not valid, 'd' is. Please make a runnable example. We don't enjoy fixing minor typos just because you wanted to save some time. - Error - Syntactical Remorse
I just missed it in all the jargon, sorry. d={} is an empty dictionary to start with. - TR007

1 Answers

1
votes

I don't know where dict3 comes into play with your desired output but this will get the desired output:

dict1 = {'a':['d','e','f'], 'b':['p','q','r']}
dict2 = {'d':100, 'e':100, 'f':100, 'p':100, 'q':100, 'r':100}

result = {key1: {key2: dict2[key2] for key2 in val1} for key1, val1 in dict1.items()}
print(result)

Output:

{
  "a": {
    "d": 100,
    "e": 100,
    "f": 100
  },
  "b": {
    "p": 100,
    "q": 100,
    "r": 100
  }
}