Open felipe74590 opened 3 days ago
# Routing numbers, unique to a bank but each bank can have many routing numbers, but also can only correspond to one bank.
## Bank names, multiple names valid for bank
## Bank Ids, only one and its unique (integer)
new_rn_to_name = [
{
"123": "Wells Fargo",
"456": "Chase",
},
{
"123": "Wells",
"789": "Capital One",
"456": "Bank of America",
},
{
"123": "Bank of America",
"456": "Chase",
},
]
# rn_to_name= {
# "123": "Wells Fargo",
# "456": "Chase",
# "789": "Capital One",
# "555": "First State Bank",
# }
name_to_bank_id = [
# There are multiple common ways to write the name of this bank
("Wells Fargo", 1),
("Wells", 1),
("Chase", 2),
("Capital One", 3),
("Bank of America", 4),
# These are two different banks with the same name
("First State Bank", 5),
("First State Bank", 6),
]
# Example output
# 123 -> 1
# 456 -> 2
# 789 -> 3
# 555 -> 5,6
from typing import Dict, List, Tuple
from collections import defaultdict
# create_routing_number_mapping combines a map from routing number to bank name with a list of relationships between
# bank names and bank IDs to create a single map with routing numbers as keys and a list of related bank IDs as values.
def create_routing_number_mapping(rn_to_name: Dict[str, str], name_to_bank_id: List[Tuple[str, int]]) -> Dict[str, List[int]]:
"""returning dict {"route_number": bank_ids(list)}"""
# try to find the banks with the same ids
# possible names {}, and bank name sharing id with another bank name will be added to this key = bank id, value list of bank names
mapping_ids = defaultdict(list)
final_result = {}
for bank in name_to_bank_id:
mapping_ids[bank[0]].append(bank[1])
for key, name in rn_to_name.items():
if name in mapping_ids:
final_result[key] = mapping_ids[name]
else:
print("Bank name does not match any current banks listed.")
return final_result
def check_each_source(new_rn_to_name):
"""return best mapping"""
list_of_mappings = {}
for db in new_rn_to_name:
this_map = create_routing_number_mapping(db, name_to_bank_id)
for key, value in this_map.items():
for id in value:
if key in list_of_mappings and id not in list_of_mappings[key]:
list_of_mappings[key][id] = 1
elif key not in list_of_mappings:
list_of_mappings[key] = {id: 1}
else:
new_count = list_of_mappings[key][id] + 1
list_of_mappings[key][id] = new_count
print(list_of_mappings)
for bank_ids, value in list_of_mappings.items():
correct_route_num = max(value, key=value.get)
print(correct_route_num)
print(check_each_source(new_rn_to_name))
@bbelderbos
Thanks, I will take a look this week.