CSV: how to add columns to list and find best match (closest value) from another list?

Viewed 83

I have a code that reads a column in a CSV file with 3 columns: Zone, Offnet calls, and Traffic. enter image description here

I need the code that will create a list from rows: for example line 3 will have a list of [421, 30167] and it will search the best match (closest value) from the list provided from the code:

tp_usp15 = [10, 200]
tp_usp23 = [15, 250]
tp_usp27 = [20, 300]
list_usp = [tp_usp15,tp_usp23, tp_usp27]

tp_bsnspls_s = [1,30]
tp_bsnspls_steel = [13,250]
tp_bsnspls_chrome = [18,350]
list_bsnspls = [tp_bsnspls_s,tp_bsnspls_steel,tp_bsnspls_chrome]

tp_bsnsrshn10 = [10,200]
tp_bsnsrshn15 = [15,300]
tp_bsnsrshn20 = [20,400]
list_bsnsrshn = [tp_bsnsrshn10,tp_bsnsrshn15,tp_bsnsrshn20]

common_list = list_usp + list_bsnspls + list_bsnsrshn

So, I need a code that will compare rows from CSV to this list. For example, I have a code that works with the close concept but it works with inputs provided by the user.

client_traffic = int(input("Enter the expected monthly traffic: "))
client_offnet = int(input("Enter monthly offnet calls: "))
list_client = [client_payment, client_offnet]

from functools import partial
def distance_squared(x, y):
    return (x[0] - y[0])**2 + (x[1] - y[1])**2
best_match_overall = min(common_list, key=partial(distance_squared, list_client))
name_best_match_overall = [k for k,v in locals().items() if v == best_match_overall][0]

How can I update this code to work with every row from the CSV file? Please, help.

0 Answers
Related