I want to one group where all values are with 30% of each other.
working code is follows :
working code is follows :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from itertools import combinations def pctDiff(A,B): return abs (A - B) * 200 / (A + B) def main(): dict2 = {} dict = { 'acct_number' : 10202 , 'acct_name' : 'abc' , 'v1_rev' : 3000 , 'v2_rev' : 4444 , 'v4_rev' : 234534 , 'v5_rev' : 5665 , 'v6_rev' : 66 , 'v7_rev' : 66 , 'v3_rev' : 66 } vendors_revenue_list = [ 'v1_rev' , 'v2_rev' , 'v3_rev' , 'v4_rev' , 'v5_rev' , 'v6_rev' , 'v7_rev' , 'v8_rev' ] #prepared list of vendors for k in vendors_revenue_list: if k in dict .keys(): dict2.update({k: dict [k]}) print (dict2) #provides all possible combination for a, b in combinations(dict2, 2 ): groups = [(a,b) for a,b in combinations(dict2, 2 ) if pctDiff(dict2[a],dict2[b]) < = 30 ] print (groups) |
Output:{'v1_rev': 3000, 'v2_rev': 4444, 'v3_rev': 66, 'v4_rev': 234534, 'v5_rev': 5665, 'v6_rev': 66, 'v7_rev': 66}
[('v2_rev', 'v5_rev'), ('v3_rev', 'v6_rev'), ('v3_rev', 'v7_rev'), ('v6_rev', 'v7_rev')]
desired output Output:[('v2_rev', 'v5_rev'), ('v3_rev', 'v6_rev','v7_rev')]