Checking for duplicate items in lists in a dictionary in Python

I have a dictionary that can have as many as N keys, with a list attached to each key. I need to check for unique items in one list, from all other lists in the dictionary.

Dictionary structure example:

dict = {'N1': ['e1', 'e2', ...], 'N2': ['e1', 'e3', ...], 'N...': [....], ....}

Then I need to add them to another dictionary under the same key, but only with unique elements in the list

return_dict = {'N1': ['e2'], 'N2': ['e3'], 'N...': [...], ...}

I hope there is enough information here to understand what I am asking. If someone can help me figure out a way to easily do this, it will be very appreciated.

Edit: I am also forbidden to import anything. Today there is no convenient import .: (

+4
source share
3

, (sigh), "" , , :

d = {'N1': ['e1', 'e2'], 'N2': ['e1', 'e3']}

inverse = {item: [key for key,ls in d.items() if item in ls]
           for item in set(sum(d.values(),[]))
          }

inverse {'e1': ['N1', 'N2'], 'e3': ['N2'], 'e2': ['N1']}. "" , , :

ret = {key: [item for item in count if key in count[item] and len(count[item]) == 1]
       for key in set(sum(count.values(),[]))
       }

ret {'N1': ['e2'], 'N2': ['e3']}.


, , itertools.chain.from_iterable lambda x: sum(x, []): sum , "" , .

+2

-, dict , .

, , , :

from collections import Counter
d = {'N1': ['e1', 'e2'], 'N2': ['e1', 'e3']}
# Copy the dictionary if you want to keep the original one. Note in particular
# that `return_dict = dict(d)` will not be sufficient as we need a deep copy
return_dict = {k: list(v) for k, v in d.items()}
# Count the occurrences of list elements
counts = Counter([a for v in return_dict.values() for a in v])
# Remove elements that appear more than once
for v in return_dict.values():
    for a in v:
        if counts[a] > 1:
            v.remove(a)
print(d)  # Prints {'N2': ['e1', 'e3'], 'N1': ['e1', 'e2']}
print(return_dict)   # Prints {'N1': ['e2'], 'N2': ['e3']}
+1

Well, going from the answer of L3viathan, I started to take a similar approach. It will be really scruffy and terrible, but it works for my needs. What I found to work was as follows:

ret = {}
seen = set()
repeated = set()
for key in hash_dict:
    for item in hash_dict[key]:
        if item in seen:
            repeated.add(item)
        else:
            seen.add(item)
for key in hash_dict:
    ret[key] = ret.get(key, [])
    for item in hash_dict[key]:
        if item not in repeated:
            ret[key].append(item)
return ret

It looks very messy for me, but since I don't have extensive knowledge of python and am limited by the inability to use imports, this is what I could do.

Edit: different variable names look like I copied this directly from my project I was working on.

0
source

Source: https://habr.com/ru/post/1662200/


All Articles