gpt4 book ai didi

mysql - 如何以Python方式匹配和分配数据?

转载 作者:行者123 更新时间:2023-11-29 06:08:11 24 4
gpt4 key购买 nike

我有一个人员列表(mysql 表)及其标题,如下表所示。我还有一个标题及其类别的列表。我如何将他们的类别分配给该人?当一个人有多个头衔时就会出现问题。将标题映射到类别并将其分配给人员的 Python 方式是什么?

人员表

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

最佳答案

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,那就太好了。

关于mysql - 如何以Python方式匹配和分配数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com