Python create dictionary with duplicates entries

Question:

I have the following txt file:

Site1 Manager1
Site2 Manager2
Site3 Manager2
Site4 Manager3
Site5 Manager3
Site6 Manager3

and you can see some managers have multiple sites. So I need to create a dictionary based on the managers. So I was wondering if possible to write a script to give me this output:

{   'Manager1':'Site1',
    'Manager2':'Site2','Site3',
    'Manager3':'Site4','Site5','Site6',
}

The current script I have, is actually working but it’s not categorizing based on the manager. It’s doing it based on Site.

for key, value in allsiteDic.items():
    rev_dict.setdefault(value, set()).add(key)
      
result = [key for key, values in rev_dict.items()
                              if len(values) > 1]
  
print("duplicate values", str(result))
print(allsiteDic)

Output:

{'Site6': 'Manager3', 'Site4': 'Manager3', 'Site5': 'Manager3', 'Site2': 'Manager2', 'Site3': 'Manager2', 'Site1': 'Manager1'}
Asked By: user15109593

||

Answers:

Yes, you need to read the txt into the memory, and then do something like:

from collections import defaultdict

# read file and put its contents into "lines"
with open('myfile.txt', 'r') as f: 
    lines = f.readlines()

# initialise defaultdict to store data

res = defaultdict(list)

for line in lines:
    site, manager = line.split(' ') # get site and manager from string
    res[manager].append(site)

print(res)
Answered By: svfat

Try following to reverse the dictionary :

my_dict = {'Site1' : 'Manager1', 'Site2' : 'Manager2', 'Site3' : 'Manager2',
'Site4' : 'Manager3',
'Site5' : 'Manager3',
'Site6' : 'Manager3'}

my_dict2 = {y: x for x, y in my_dict.items()}

print(my_dict2)
Answered By: Drp RD

without defaultdict and removing extra n.

final_dict = {}
with open("file.txt", "r") as f:
    for l in f.readlines():
        site, manager = l.split(" ")
        manager = manager.replace("n", "")
        if manager not in final_dict.keys():
            final_dict[manager] = [site]
        else:
            final_dict[manager].append(site)
print(final_dict)
{'Manager1': ['Site1'], 'Manager2': ['Site2', 'Site3'], 'Manager3': ['Site4', 'Site5', 'Site6']}
Answered By: A D

You probably want a list of sites per manager so…

result = dict()

with open('foo.txt') as indata:
    for site, manager in map(str.split, indata):
        result.setdefault(manager, []).append(site)

print(result)

Output:

{'Manager1': ['Site1'], 'Manager2': ['Site2', 'Site3'], 'Manager3': ['Site4', 'Site5', 'Site6']}
Answered By: OldBill
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.