-1

I created two dictionaries. Each is based on a different query of the same database. There is a key and four fields from the database in each dictionary. I want to find all rows of dict_x that are in are in dict_y.

 for row in dict_x:
    if dict_y.values() not in dict_x.values():
         del dict_x[row]

print 'Length dict_x', len(dict_x)

This returns the error

TypeError: 'type' object does not support item deletion
  • 3
    What do you mean by "row"? Dictionaries don't have rows. They have keys and values. – jpp Jan 21 '19 at 17:24
  • Also, you're comparing `dict_y.values()` with `dict_x.values()`. This is comparing the entire dictionary's values on each iteration - surely not what you want – Adam Hughes Jan 21 '19 at 17:25
  • 2
    By row i mean dictionary item-- I used the word row because the dictionaries were formed from SQL queries. – Anne Gilligan Jan 21 '19 at 17:25
  • won't the dictionary size change during iteration here or am I missing something – gold_cy Jan 21 '19 at 17:26
  • So if the values of a key/row of `dict_x` are in `dict_y` then you want to remove the `key` from `dict_x`? – Buddy Lindsey Jan 21 '19 at 17:27
  • `if dict_y.values() not in dict_x.values()` should likely be replaced with `if dict_x[row] not in dict_y.values()` – KuboMD Jan 21 '19 at 17:28
  • so if it's a list of dictionary you code looks more or less good, you are doing del dict_x[row] but you can't pass the row, you need pass the row index; but be careful, you are deleting from the same list that you are iteration so it will cause the jump of a line, because if you delete row 2, row 3 become row 2 and next iteration you will lose it because next iteration will be on row 3 – Carlo 1585 Jan 21 '19 at 17:30
  • I want rows with the sames values, their keys will be different. – Anne Gilligan Jan 21 '19 at 17:31
  • I would also be okay with creating a new dictionary with the output. – Anne Gilligan Jan 21 '19 at 17:32
  • I was just also having issues with appending these "rows" to a new dict. – Anne Gilligan Jan 21 '19 at 17:32
  • give example input and output in your questions so as to make it easier for the ones who answer – Kannappan Sirchabesan Jan 21 '19 at 18:25

4 Answers4

0

The steps to solve the problem would be to

  1. Invert the key value pairs of the dictionaries
  2. Identify the common intersecting keys
  3. Loop through the keys and check if their values match

The code could look something like below

dict_x = {v: k for k, v in dict_x.items()}
dict_y = {v: k for k, v in dict_y.items()}

for key in dict_x.keys() & dict_y.keys():
   print(key, dict_x[key])
   print(key, dict_y[key])

Here is the dict comprehension equivalent in python 3

result_set = {key: dict_x[key] for key in dict_x.keys() & dict_y.keys() if dict_x[key] == dict_y[key]}
Kannappan Sirchabesan
  • 1,353
  • 11
  • 21
0
>>> dict_a = {'a':[1,1,2,3]}
>>> dict_b = {'b':[1,2]}
>>> for a_key, b_key in zip(dict_a.keys(), dict_b.keys()):
...     print [i for i in dict_a[a_key] if i in set(dict_b[b_key])]
... 
[1, 1, 2]
0

This will work as long as the elements in the array will always be in the same order.

dict_x = {'hi': ['hello', 'hi'], 'bye': ['good bye', 'bye']}
dict_y = {'hi': ['hello', 'hi']}
dict_z = dict()

for key, row in dict_x.items():
    if row in dict_y.values():
         dict_z[key] = row
print(dict_z)

If the elements won't be in the same order then you'll have to do this:

dict_x = {'hi': ['hi', 'hello'], 'bye': ['good bye', 'bye']}
dict_y = {'hi': ['hello', 'hi']}
dict_z = dict()

for x_key, x_row in dict_x.items():
    for y_key, y_row in dict_y.items():
        if set(x_row).intersection(y_row):
            dict_z[x_key] = y_row

print(dict_z)
jonyfries
  • 772
  • 6
  • 20
0

This might help, it will return False if the dict is equal and true if not. I know its other way around

def compare_dict(
    dict_1: Dict[Any, Any], dict_2: Dict[Any, Any]
):
    new_key = any([False if key in dict_1 else True for key in dict_2])
    delete_key = any(
        [False if key in dict_2 else True for key in dict_1]
    )

    if new_key or delete_key:
        return True
    else:
        values_mismatch_flag = any(
            [
                True if v != dict_1[k] else False
                for k, v in dict_2.items()
            ]
        )
        if values_mismatch_flag:
            return True
        return False
sandy
  • 11
  • 2