python - Combine two lists based on a column -
i trying combine 2 lists based on field (similar performing inner join)
list : name, position, employee id
sample entries
1) bob, admin, 32443
2) jack, security, 5464
list b: position, tasks
sample entries
1) admin, check system files
2) admin, add users
3) admin, delete users
4) security, perform review
5) security, check settings
i need final output this:
bob, admin, 32443, check system files
bob, admin, 32443, add users
bob, admin, 32443, delete users
jack, security, 5464, perform review
jack, security, 5464, check settings
please guide me how include such code in simple loops. new python thank in advance
take cartesian product, skip items fail join condition:
((name, position, emp_id, tasks) name, position, emp_id in lista position2, tasks in listb if position == position2)
if use itertools
, can shorten avoid nested for
loops:
((name, position, emp_id, tasks) (name, position, emp_id), (position2, tasks) in itertools.product(lista, listb) if position == position2)
both of above snippets generator expressions. if want real list, change outermost pair of parentheses square brackets.
Comments
Post a Comment