0

我有一个人员列表(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
4

4 回答 4

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']}
于 2012-05-03T19:47:36.603 回答
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

于 2012-05-03T19:49:04.427 回答
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']}}
于 2012-05-03T19:57:10.717 回答
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']
于 2012-05-03T19:53:05.000 回答