0

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'}
user15109593
  • 105
  • 5

4 Answers4

1

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']}
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

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)
svfat
  • 3,273
  • 1
  • 15
  • 34
0

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)
Drp RD
  • 190
  • 1
  • 1
  • 6
  • Each `y: x` assignation overwrites the previous one with the same `y`. The output is `{'Manager1': 'Site1', 'Manager2': 'Site3', 'Manager3': 'Site6'}`. – Ignatius Reilly Oct 03 '22 at 15:44
0

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']}
A D
  • 585
  • 1
  • 7
  • 16
  • If you call str.split without any arguments it splits on whitespace which would have the effect of removing newline. Consider using setdefault – DarkKnight Oct 03 '22 at 15:58
  • thnx for the point, I tried to answer fast, so I missed this. @OldBill – A D Oct 03 '22 at 16:01