-1

Case 1: I wanna filter Dict1 using the value of Dict2, Ex : filter value >1 using value of dict2

 Dict1={'saya': 0.1823215567939546,
     'menolak': 0.6931471805599453,
     'omnibus': -0.15415067982725836,
     'law': -0.15415067982725836,
     'dan': 1.0986122886681098,
     'tetap': 1.0986122886681098,
     'di': 1.0986122886681098,
     'sekolah': 1.0986122886681098,
     'diperintahkan': 1.0986122886681098,
     'untuk': 1.0986122886681098,
     'tidak': 1.0986122886681098,
     'layak': 1.0986122886681098,
     'meningkatkan': 1.0986122886681098,
     'kesejahteraan': 1.0986122886681098,
     'setuju': 0.6931471805599453,
     'diterapkan': 0.6931471805599453}

Dict2={'saya': 1,
 'menolak': 2,
 'omnibus': 3,
 'law': 1,
 'dan': 0.5,
 'tetap': 0.5,
 'di': 1,
 'sekolah': 2,
 'diperintahkan': 3,
 'untuk': 4,
 'tidak': 5,
 'layak': 1.0986122886681098,
 'meningkatkan': 6,
 'kesejahteraan': 5,
 'setuju': 8,
 'diterapkan': 2}

The result that i hope:

Result={
         'menolak': 0.6931471805599453,
         'omnibus': -0.15415067982725836,
         'sekolah': 1.0986122886681098,
         'diperintahkan': 1.0986122886681098,
         'untuk': 1.0986122886681098,
         'tidak': 1.0986122886681098,
         'meningkatkan': 1.0986122886681098,
         'kesejahteraan': 1.0986122886681098,
         'setuju': 0.6931471805599453,
         'diterapkan': 0.6931471805599453 }

Case 2: I wanna filter Dict1 using the value of Dict2, Ex : filter the 5 best value using value of dict2

The result that i hope:

Dict2={
 'untuk': 1.0986122886681098,
 'tidak': 1.0986122886681098,
 'meningkatkan': 1.0986122886681098,
 'kesejahteraan': 1.0986122886681098,
 'setuju': 0.6931471805599453,
}
  • What have you tried yourself? What problem did you run into? Currently, your question looks very much like you're asking SO to write your code for you? – Grismar Oct 08 '20 at 04:55
  • [create-a-dictionary-with-list-comprehension](https://stackoverflow.com/questions/1747817/create-a-dictionary-with-list-comprehension) – Patrick Artner Oct 08 '20 at 04:58
  • [how-can-i-use-if-else-in-a-dictionary-comprehension](https://stackoverflow.com/questions/9442724/how-can-i-use-if-else-in-a-dictionary-comprehension) – Patrick Artner Oct 08 '20 at 04:59

2 Answers2

0

You can achieve using the following -

1st Result:

Result_1 = {key: value for key, value in Dict1.items() if Dict2.get(key, 0) > 1}

{'menolak': 0.6931471805599453,
'omnibus': -0.15415067982725836,
'sekolah': 1.0986122886681098,
'diperintahkan': 1.0986122886681098,
'untuk': 1.0986122886681098,
'tidak': 1.0986122886681098,
'layak': 1.0986122886681098,
'meningkatkan': 1.0986122886681098,
'kesejahteraan': 1.0986122886681098,
'setuju': 0.6931471805599453,
'diterapkan': 0.6931471805599453}

2nd Result:

 Result_2 = {k: Dict1[k] for k in sorted(Dict2.keys(), key = lambda x: Dict2[x], reverse = True)[:5] if k in Dict1}

{'setuju': 0.6931471805599453,
 'meningkatkan': 1.0986122886681098,
 'tidak': 1.0986122886681098,
 'kesejahteraan': 1.0986122886681098,
 'untuk': 1.0986122886681098}
Dev
  • 665
  • 1
  • 4
  • 12
0

try this

result = {}

for key1, value1 in Dict1.items():
    for key2, value2 in Dict2.items():
        if Dict2[key2] > 1:
            result[key1] = Dict1[key1]
Carlos
  • 452
  • 2
  • 18