python函数通过outs的字典循环删除和添加仅更新某些项目的项目
事先感谢您的帮助。
我有两个词典: parent_dict 具有一系列值为& pub_plat_dict 是一部查找词典,旨在纠正构成 parent_dict 的项目的名称。
函数 update_dict 允许名称找到'。到通过。如果它找不到名称,则将尝试在 pub_plat_dict 中找到名称。如果发现它将 .emove 旧名称和 .add 更新的名称。如果不存在名称,那么我希望该程序移至下一个项目。
当我运行函数 update_dict 纠正 parent_dict 中的第一项时,准确地跳过了不需要更新但不需要更新但不需要的多个项目或 .add 另一个错误命名的项目。
样本数据
parent_dict = {
'49d238407e0102ba':{'opportunity_history'}
, 'f9d53c74ec1d2ff6':{'servicer.trial_balance','src_platform.loan','src_platform.loan_disbursement'}
, 'fc35a98e0cfaab3d':{'loan', 'loan_agreement', 'opportunity_compliance_flag','zodiac'}
}
pub_plat_dict = {'loan':'src_platform.loan',
'opportunity_compliance_flag':'src_platform.opportunity_compliance_flag',
'opportunity_history':'src_platform.opportunity_history',
'loan_agreement': 'src_platform_mosaic_live.loan_agreement'}
函数
def update_dict(parent_dict):
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find(".") != -1:
pass
else:
try:
update = pub_plat_dict[tbl]
tbls.remove(tbl)
tbls.add(update)
except:
pass
return(parent_dict)
输出
{'49d238407e0102ba': {'src_platform.opportunity_history'}, 'f9d53c74ec1d2ff6': {'src_platform.loan', 'src_platform.loan_disbursement', 'servicer.trial_balance'}, 'fc35a98e0cfaab3d': {'opportunity_compliance_flag', 'loan_agreement', 'loan', 'zodiac'}}
注意:第一项已正确更新,但其他所有内容都没有变化。
我进行了以下循环以尝试找出我的错误(将其保持在 update_dict 代码的近距离之外)。
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find('.') != -1:
print("UNCHANGED-" + tbl)
else:
try:
print("CHANGED-" + pub_plat_dict[tbl])
except:
print("FAILURE-"+ tbl)
它为我提供了以下输出:
UNCHANGED-src_platform.opportunity_history
UNCHANGED-src_platform.loan
UNCHANGED-src_platform.loan_disbursement
UNCHANGED-servicer.trial_balance
CHANGED-src_platform.opportunity_compliance_flag
CHANGED-src_platform_mosaic_live.loan_agreement
CHANGED-src_platform.loan
FAILURE-zodiac
除了大写字眼外,这是我期望父母的外观。因此,我的 .emove 和 .add 无法持续工作。
编辑:我还替换了 .discard .emove ,输出没有更改。
任何帮助将不胜感激。
Thanks in advance for your assistance.
I have two dictionaries: parent_dict has a series of sets as values & pub_plat_dict is a look-up dictionary that seeks to correct the names of the items that make up the sets in parent_dict.
The function update_dict allows names where it finds a '.' to pass. If it doesn't find a name then it will try to find the name in the pub_plat_dict. If found it will .remove the old name and .add the updated name. If the name isn't present, then I want the program to move to the next item.
When I run the function update_dict corrects the first item in the parent_dict, accurately skips multiple items that don't need to be updated but then doesn't .remove or .add the other wrongly named items.
Sample Data
parent_dict = {
'49d238407e0102ba':{'opportunity_history'}
, 'f9d53c74ec1d2ff6':{'servicer.trial_balance','src_platform.loan','src_platform.loan_disbursement'}
, 'fc35a98e0cfaab3d':{'loan', 'loan_agreement', 'opportunity_compliance_flag','zodiac'}
}
pub_plat_dict = {'loan':'src_platform.loan',
'opportunity_compliance_flag':'src_platform.opportunity_compliance_flag',
'opportunity_history':'src_platform.opportunity_history',
'loan_agreement': 'src_platform_mosaic_live.loan_agreement'}
Function
def update_dict(parent_dict):
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find(".") != -1:
pass
else:
try:
update = pub_plat_dict[tbl]
tbls.remove(tbl)
tbls.add(update)
except:
pass
return(parent_dict)
Output
{'49d238407e0102ba': {'src_platform.opportunity_history'}, 'f9d53c74ec1d2ff6': {'src_platform.loan', 'src_platform.loan_disbursement', 'servicer.trial_balance'}, 'fc35a98e0cfaab3d': {'opportunity_compliance_flag', 'loan_agreement', 'loan', 'zodiac'}}
NOTE: the first item is updated correctly but everything else is left unchanged.
I did the following loop to try to figure out my error (keeping it as close to the update_dict code as I could).
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find('.') != -1:
print("UNCHANGED-" + tbl)
else:
try:
print("CHANGED-" + pub_plat_dict[tbl])
except:
print("FAILURE-"+ tbl)
It gives me the following output:
UNCHANGED-src_platform.opportunity_history
UNCHANGED-src_platform.loan
UNCHANGED-src_platform.loan_disbursement
UNCHANGED-servicer.trial_balance
CHANGED-src_platform.opportunity_compliance_flag
CHANGED-src_platform_mosaic_live.loan_agreement
CHANGED-src_platform.loan
FAILURE-zodiac
Aside from the capitalized- word this is what I would expect the parent_dict would now look like. So my .remove and .add aren't working consistently.
EDIT: I also substituted .discard for .remove, the output did not change.
Any assistance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法在创建它时使该功能工作。问题的一部分是
返回
语句出现的位置。即使迭代仍未完成,使用返回
即使迭代仍未完成,也会将其打破并退出功能。另一个问题可能是功能中的冗余逻辑。我决定创建一个新的词典,并将列表用作DICT的值而不是集合。我简化了该功能中的逻辑,并从语句中摆脱了
,根据
尝试
子句,这似乎是多余的。当我运行功能时,我会得到我期望的:
函数输出
总体而言,更改似乎适用于数据集中的100K项目。
感谢所有人的看看。
I couldn't get the function to work as I created it. Part of the issue is where the
return
statement appears. Using areturn
inside of a loop will break it and exit the function even if the iteration is still not finished. Another issue might be the redundant logic in the function.I decided to create a new dictionary and use a list as the dict's values instead of sets. I simplified the logic in the function and got rid of the
if
statement which seemed redundant in light of thetry
clause.when I run the function I get what I expect:
Function Output
Overall the change seems to work for the 100K items in the dataset.
Thanks to everyone for taking a look.