0

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]}
2
  • 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. Commented Jun 14, 2019 at 15:23
  • I just missed it in all the jargon, sorry. d={} is an empty dictionary to start with. Commented Jun 14, 2019 at 17:16

1 Answer 1

1

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
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, Appreciate this but I am not looking to form a nested dictionary, instead wanted to compare them as stated in my question and re-group them accordingly. If you see clearly dict3 is clearly declared and has same keys dict2 but with different attributes. Want to do below. d = { a:{d:ver1, e:ver2, f:ver3}, b:{p:ver4, q:ver5, r:ver6 } after sorted internally to something like if d's ver1 = e's ver2 =! f's ver3 dict4 = {a: { equal:[d,e], unequal: [f]}
@TR007 Break your question into "known inputs" and "desired outputs" along with how you get there. Your question isn't that clear, it is hard to see what is an input and what you derived (since you define all of them). I have no clue where the ver stuff comes from in your question.
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
I don't think you reviewed the code snippet in detail which contains 'ver'. Anyways, I want to break it down to this, ignoring the rest of the question, I have a dictionary d = { a:{d:val1, e:val2, f:null}, b:{p:val3, q:val4, r:val5 }}. So I want to take value of each key and compare the nested values between themselves and if val1 = val2, and val3=val4 the desired output is new_dict={a:{equal: [d,e], null:f}, b:{equal:[p,q], unequal:r}}. so the values have to be compared only with the rest of the values under same nested key and get re-organized into a new_dict

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.