I want to display the key value of users based on role hierarchy
users = {'Jhon': 'CEO', 'Matew': 'CTO', 'Tom': 'Tester'}
keys_list = list(users)
print(keys_list[0],"-")
itercars = iter(keys_list)
next(itercars)
for car in itercars:
print(car)
Considering the users, I'm trying to get an output such as
Expected Output
Jhon - Matew ,Tom
Matew - Tom
Tom - N/A
I need to prioritize the users according to the role
since Jhon is CEO the output should show the people under Jhon
Thanks in advance
CodePudding user response:
You could create a position to level mapping using the last seen index for a position:
# users = {'Jhon': 'CEO', 'Matew': 'CTO', 'Tom': 'Tester'}
users = {'Jhon': 'CEO', 'Matew': 'CTO', 'Sarah': 'CTO', 'Tom': 'Tester', 'Wick': 'Tester'}
position_to_level = {}
level_to_position = {}
for level, position in enumerate(reversed(users.values())):
if position not in position_to_level:
position_to_level[position] = level
level_to_position[level] = position
levels = [level for level in level_to_position]
level_to_reports = {level: [] for level in levels}
for user, position in users.items():
level = position_to_level[position]
for other_level in levels:
if level < other_level:
level_to_reports[other_level].append(user)
for user, position in users.items():
level = position_to_level[position]
reports = level_to_reports[level]
print(f'{user} - {", ".join(reports) if reports else "N/A"}')
Output when users = {'Jhon': 'CEO', 'Matew': 'CTO', 'Tom': 'Tester'}
:
Jhon - Matew, Tom
Matew - Tom
Tom - N/A
Output when users = {'Jhon': 'CEO', 'Matew': 'CTO', 'Sarah': 'CTO', 'Tom': 'Tester', 'Wick': 'Tester'}
:
Jhon - Matew, Sarah, Tom, Wick
Matew - Tom, Wick
Sarah - Tom, Wick
Tom - N/A
Wick - N/A