Try this:
myl=dict() with open('test.txt') as f : for l in f: l = l.rstrip() try: tags = l.split('/')[1] myl.setdefault(tags,[]) myl[tags].append(l.split('/')[0]) for t in tags: myl.setdefault(t,[]) myl[t].append( l.split('/')[0]) except: pass keys=myl.keys() for k1 in keys: for k2 in keys: if len(set(k1).intersection(k2))==len(set(k1)) and k1!=k2: myl[k1].extend([myk2v for myk2v in myl[k2] if myk2v not in myl[k1]]) print(myl)
Output
{'S': ['test', 'test', 'girl', 'house', 'wind'], 'SE': ['girl', 'house', 'wind'], 'E': ['girl', 'house', 'man', 'man', 'wind'], '': ['home', 'test', 'test', 'girl', 'house', 'wind', 'man', 'man'], 'SE123': ['house'], '1': ['house'], '2': ['house'], '3': ['house'], 'ES': ['wind', 'girl', 'house']}
In the last two for loops of the program, the set ofk1andk2are taken first, and then, the intersection of the two sets is compared. If the length of the intersection is equal to the length of setk1, the value of the keyk2should be in the keyk1, so the value of the keyk2is added to the keyk1.