使用纯 python 在 python 中的两个嵌套数据结构之间执行比较并赋值

发布于 01-18 05:09 字数 1170 浏览 2 评论 0原文

我目前有两个数据集(数据库、作业),分别是嵌套列表字典和字典列表。我想使用数据库中特定键的 vlookup 函数,如果键:值与分配中的键匹配,我想在原始数据库中创建一个新的键值对(有点为某人分配一个根据他们的工作范围来完成的任务)。 我想使用纯 python 来完成此操作,而不导入任何库(没有 pandas/numpy 等)。

为了更清楚地解释一下,这是我的代码以及我试图实现的目标:

database = \[{‘name’ : ’ABC’, ‘position’ : ‘executive’}, {‘name’ : ’DEF’, ‘position’ : ‘senior exec}, {‘name’ : ’GEH’, ‘position’ : ‘contractor’}\]

assignment =  { 'executive': \[‘task1', ‘task2’\], 
    ‘senior exec: \[‘task3', ‘task4’\] , 
    'contractor': \[‘task5'\]}

我认为代码应该遵循类似于 if-else 逻辑vlookup,类似这样的东西 -

if database\[‘position’\] == assignment\[‘executive’\] 
    create new key – ‘jobscope’ and assign task1, task2 as a list etc.

我期望的输出是这样的:

database = \[{‘name’ : ’ABC’, ‘position’ : ‘executive’, ‘jobscope’ : \[‘task1', ‘task2’\]}, {‘name’ : ’DEF’, ‘position’ : ‘senior exec’, ‘jobscope’ : \[‘task3', ‘task4’\]}, {‘name’ : ’GEH’, ‘position’ : ‘contractor’, ‘jobscope’ : \[‘task5'\]}\]

我不确定如何实现此循环并生成输出,而且我也有一定的灵活性来更改赋值的数据结构所以我寻求你的建议,如果dict-list 也是实现此输出的最佳方法吗?

I currently have two datasets (database, assignment) which are nested list-dict and dict-list respectively. I would like to use a sort of vlookup function of a specific key in database, and if the key:value matches a key in assignment, I would like to create a new key-value pair in the original database (sort of assigning someone a task to do, based on their jobscope).
I would like to complete this using purely python without importing any libraries (no pandas/numpy etc.)

To explain abit clearer, heres my code and what im trying to achieve:

database = \[{‘name’ : ’ABC’, ‘position’ : ‘executive’}, {‘name’ : ’DEF’, ‘position’ : ‘senior exec}, {‘name’ : ’GEH’, ‘position’ : ‘contractor’}\]

assignment =  { 'executive': \[‘task1', ‘task2’\], 
    ‘senior exec: \[‘task3', ‘task4’\] , 
    'contractor': \[‘task5'\]}

Im thinking the code should be follow an if-else logic similar to a vlookup, something like this --

if database\[‘position’\] == assignment\[‘executive’\] 
    create new key – ‘jobscope’ and assign task1, task2 as a list etc.

the output im expecting is something like this:

database = \[{‘name’ : ’ABC’, ‘position’ : ‘executive’, ‘jobscope’ : \[‘task1', ‘task2’\]}, {‘name’ : ’DEF’, ‘position’ : ‘senior exec’, ‘jobscope’ : \[‘task3', ‘task4’\]}, {‘name’ : ’GEH’, ‘position’ : ‘contractor’, ‘jobscope’ : \[‘task5'\]}\]

I’m not sure how to achieve this looping and producing output, also I have some flexibility to change the data structure of assignment so I seek your advice if a dict-list is the best way to achieve this output as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作2025-01-25 05:09:15

这个脚本应该可以工作:

database = [
    {'name': 'ABC', 'position': 'executive'},
    {'name': 'DEF', 'position': 'senior exe'},
    {'name': 'GEH', 'position': 'contractor'},
]

assignment = {
    'executive': ['task1', 'task2'],
    'senior exec': ['task3', 'task4'],
    'contractor': ['task5'],
}

for dictionary in database:
    if dictionary['position'] in assignment.keys():
        dictionary['jobscope'] = assignment[dictionary['position']]

print(database)

This script should work:

database = [
    {'name': 'ABC', 'position': 'executive'},
    {'name': 'DEF', 'position': 'senior exe'},
    {'name': 'GEH', 'position': 'contractor'},
]

assignment = {
    'executive': ['task1', 'task2'],
    'senior exec': ['task3', 'task4'],
    'contractor': ['task5'],
}

for dictionary in database:
    if dictionary['position'] in assignment.keys():
        dictionary['jobscope'] = assignment[dictionary['position']]

print(database)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文