I have a set of lists composed of names and role. However, there is no real logic between how the list are organized.
For instance, I have this list :
l = ['john', 'owner', 'mark', 'manager', 'alex', 'steve', 'employee', 'manager'] #the last role shouldn't be assigned to anyone
I want to assign each person the role that is located next to it in the list or the closest one from left to right.
The desired output should be something like :
output = [['john', 'owner'], ['mark', 'manager'], ['alex', 'employee'], ['steve', 'employee']]
My guess was to add indexes to the list elements, and than check which element based on a set of condition was closest to the list element I was looking at.
This is part of my script :
parties_role = [(idx, item) for idx,item in enumerate(l)]
for item in parties_role:
if "owner" not in str(item[1]) and 'manager' not in str(item[1]) and 'employee' not in str(item[1]):
name = str(item[1])
The rest of the code shoud look at the list items that contain the keywords owner
, manager
and employee
and assign to the current name
variable the closest role based on the index.
source https://stackoverflow.com/questions/76348553/group-element-in-list-based-on-conditions-and-indexes
Comments
Post a Comment