0

I have a scenario where I am trying to match below elements in json data.

a={'n_name': 'APP-Ptyh ', 'n_name': 'Com Don Ctes - Aty ', 'n_name': 'Sce Fbh - Dor Node', 'n_name': 'kke gg - Dor Node'}
b={'n_name': 'APP-Ptyh', 'n_name': 'Com Don Ctes - Aty', 'n_name': 'Sce Fbh - Dor Nomk ', 'n_name': 'kke gg - Dor Node'}
c=[]

What I have done to match :

if a["n_name"] == b["n_name"]:
   c.append(b["n_name"])

This doesn't matched the elements properly as in a data source the elements APP-Ptyh is same in both but there is a trailing whitespace. What is expected output is it should match irrespective of trailing white spaces. Any help on how can I achieve that. This is the desired output of matched elements t be stored in list c :

c= ['APP-Ptyh', 'Com Don Ctes - Aty', 'kke gg - Dor Node']
John
  • 147
  • 1
  • 10
  • 1
    If those keys are just example, you can use [strip](https://docs.python.org/3/library/stdtypes.html#str.strip) to remove the trailing whitespace. `if a["n_name"].strip() == b["n_name"].strip` – Rieljun Liguid Mar 10 '20 at 05:12
  • 3
    Your dictionaries have duplicate keys, so your if statement will only compare the last elements in dict a and b. Please fix or rename your dictionaries for further help – Lambo Mar 10 '20 at 05:12
  • [Related](https://stackoverflow.com/questions/14902299/json-loads-allows-duplicate-keys-in-a-dictionary-overwriting-the-first-value). – hilberts_drinking_problem Mar 10 '20 at 05:16
  • A dictionary can not have the same key and different value, now what are you going with ```a["n_name"]``` ??? – Aven Desta Mar 10 '20 at 05:20

3 Answers3

0

Your dictionary has duplicated keys, which not suitable. Dictionary must have unique keys (as it refers key). So, you may use the below implementation to your purpose.

Code:

a={'n_name': ['APP-Ptyh ', 'Com Don Ctes - Aty ', 'Sce Fbh - Dor Node', 'kke gg - Dor Node']}
b={'n_name': ['APP-Ptyh', 'Com Don Ctes - Aty', 'Sce Fbh - Dor Nomk ', 'kke gg - Dor Node']}
c=[]

for val in a['n_name']:
    if val in b['n_name']:
        c.append(b["n_name"][b["n_name"].index(val)])

Result:

['APP-Ptyh', 'Com Don Ctes - Aty', 'kke gg - Dor Node']

This works but not a good time complexity. It is better to give unique keys if would.

furkanayd
  • 811
  • 7
  • 19
0

You can't have a dict with duplicate keys. Instead, you can use a single key and, as value, a list of elements that had that key.

So you can follow these steps:

1.See if the current element's (of your initial set) key is in the final dict. If it is, go to step 3

2.Update dict with key

3.Append the new value to the dict[key] list

4.Repeat [1-3]

0

Make sure the dictionary keys must be unique.

a={'n_name_1': 'APP-Ptyh ', 'n_name_2': 'Com Don Ctes - Aty ', 'n_name_3': 'Sce Fbh - Dor Node', 'n_name': 'kke gg - Dor Node'}
b={'n_name_1': 'APP-Ptyh', 'n_name_2': 'Com Don Ctes - Aty', 'n_name_3': 'Sce Fbh - Dor Nomk ', 'n_name': 'kke gg - Dor Node'}

def compare_value(key1, key2):
    if key1.strip() == key2.strip():
        return key1.strip()

filter(lambda x: x != None, map(compare_value, a.values(), b.values()))

>>>['APP-Ptyh', 'Com Don Ctes - Aty', 'kke gg - Dor Node']
Veera Balla Deva
  • 790
  • 6
  • 19