我有一个人员及其标题的列表(mysql表),如下表所示。我还有一个标题及其类别列表。如何将他们的类别分配给此人?当一个人有多个头衔时会出现问题。什么是将标题映射到类别并将其分配给该人的pythonic方式?
人员表
Name Title
--------------------
John D CEO, COO, CTO
Mary J COO, MD
Tim C Dev Ops, Director
标题类别表
Title Executive IT Other
-----------------------------
CEO 1
COO 1
CTO 1 1
MD 1
Dev Ops 1
Director 1
期望的输出:
Name Title Executive IT Other
---------------------------------------------
John D CEO, COO, CTO 1 1
Mary J COO, MD 1
Tim C Dev Ops, Director 1 1
答案 0 :(得分:1)
首先以列表字典形式排列输入数据:
>>> name_to_titles = {
'John D': ['CEO', 'COO', 'CTO'],
'Mary J': ['COO', 'MD'],
'Tim C': ['Dev Ops', 'Director']
}
然后遍历输入字典以创建反向映射:
>>> title_to_names = {}
>>> for name, titles in name_to_titles.items():
for title in titles:
title_to_names.setdefault(title, []).append(name)
>>> import pprint
>>> pprint.pprint(title_to_names)
{'CEO': ['John D'],
'COO': ['John D', 'Mary J'],
'CTO': ['John D'],
'Dev Ops': ['Tim C'],
'Director': ['Tim C'],
'MD': ['Mary J']}
答案 1 :(得分:1)
name_title = (("John D",("CEO","COO","CTO")),
("Mary J",("COO","MD")),
("Tim C",("Dev Ops","Director")))
title_cat = {"CEO": set(["Executive"]),
"COO": set(["Executive"]),
"CTO": set(["Executive"]),
"MD": set(["Executive"]),
"Dev Ops": set(["IT"]),
"Director": set(["Other"])}
name_cat = [(name, reduce(lambda x,y:x|y, [title_cat[title]for title in titles])) for name,titles in name_title]
如果union
在集合上表现得像sum
,那就太好了。
答案 2 :(得分:1)
people=['john','Mary','Tim']
Title=[['CEO','COO','CTO'],['COO','MD'],['DevOps','Director']]
title_des={'CEO':'Executive','COO':'Executive','CTO':'Executive',
'MD':'Executive','DevOps':'IT','Director':'Others'
}
people_des={}
for i,x in enumerate(people):
people_des[x]={}
for y in Title[i]:
if title_des[y] not in people_des[x]:
people_des[x][title_des[y]]=[y]
else:
people_des[x][title_des[y]].append(y)
print(people_des)
输出
{'Tim': {'IT': ['DevOps'], 'Others': ['Director']}, 'john': {'Executive': ['CEO', 'COO', 'CTO']}, 'Mary': {'Executive': ['COO', 'MD']}}
答案 3 :(得分:0)
如果你的意思是你有字符串,我建议这样做:
s = '''Name Title
--------------------
John D CEO, COO, CTO
Mary J COO, MD
Tim C Dev Ops, Director
Title Executive IT Other
-----------------------------
CEO 1
COO 1
CTO 1
MD 1
Dev Ops 1
Director 1
'''
lines = s.split('\n')
it = iter(lines)
for line in it:
if line.startswith('Name'):
break
next(it) # '--------------------'
for line in it:
if not line:
break
split = line.split()
titles = split[2:]
name = split[:2]
print ' '.join(name), titles
# John D ['CEO,', 'COO,', 'CTO']
# Mary J ['COO,', 'MD']
# Tim C ['Dev', 'Ops,', 'Director']