我写了这一职能,以便根据具体逻辑合并税收价值。 它避开税词典,寻找共同国家法典的钥匙,并具有重叠的价值。 当发现这些钥匙时,其价值被合并,重复钥匙从字典中删除。
def merge_tax_values_new_logic(tax_dict):
treated_list = set()
while True:
changed = False
for key1, value1 in list(tax_dict.items()):
country_code = key1[-2:]
print( current list : ,tax_dict)
if key1 not in treated_list:
print( current iteration key : , key1)
for key2, value2 in list(tax_dict.items()):
if key2.endswith(country_code) and key1 != key2 and any(hl_id in value2 for hl_id in value1):
tax_dict[key1].extend(value2)
tax_dict.pop(key2)
tax_dict[key1] = list(set(tax_dict[key1]))
changed = True
print( current key : , key1 , matched with key : , key2 , state of the dict after the pop : , tax_dict)
break
treated_list.add(key1)
print( treated list : , treated_list)
print( ****************************** )
if changed:
break
if not changed:
break
return tax_dict
例:
new_tax_dict = { tax1_US :[ A ], tax2_US :[ B ], tax3_US :[ A , B ]}
merge_tax_values_new_logic(new_tax_dict)
结果:
current list : { tax1_US : [ A ], tax2_US : [ B ], tax3_US : [ A , B ]}
current iteration key : tax1_US
current key : tax1_US matched with key : tax3_US state of the dict after the pop : { tax1_US : [ A , B ], tax2_US : [ B ]}
treated list : { tax1_US }
******************************
current list : { tax1_US : [ A , B ], tax2_US : [ B ]}
treated list : { tax1_US }
******************************
current list : { tax1_US : [ A , B ], tax2_US : [ B ]}
current iteration key : tax2_US
current key : tax2_US matched with key : tax1_US state of the dict after the pop : { tax2_US : [ A , B ]}
treated list : { tax2_US , tax1_US }
******************************
current list : { tax2_US : [ A , B ]}
treated list : { tax2_US , tax1_US }
******************************
{ tax2_US : [ A , B ]}
它完全依靠少数关键的小dict。 然而,当这一职能涉及法令内大量关键人物(+40k钥匙和每个关键点5个要素的平均值)时,业绩确实是一个问题。
你们是否看到其他选择?
关于