if len(result) == 0: result.append({'district': None, 'sub_district': None, 'union': None}) # when multiple district found if len(result) == 2 and sum(x != None for x in result[0].values())==1 and sum(x != None for x in result[1].values())==1: #print(result) if any(re.search(result[0]['district'].lower()+'\s*'+rd,' '+input_address+' ') for rd in self.road_suffix): result=[result[1]] if any(re.search(result[1]['district'].lower()+'\s*'+rd,' '+input_address+' ') for rd in self.road_suffix): result=[result[0]] if len(result) == 2 and sum(x != None for x in result[0].values())==1 and sum(x != None for x in result[1].values())==1: for item in self.DD: if item[1]==result[0]['district'] and item[0]==result[1]['district']: result=[result[1]] break if item[1]==result[1]['district'] and item[0]==result[0]['district']: result=[result[0]] break if len(result) > 1: flag = -1 null_flag = False for i in result: #total matched comp count for each result(dsu) cnt = sum(x != None for x in i.values()) if cnt == flag: null_flag = True break if cnt > flag: item = i flag = cnt if null_flag == False: result = [] result.append(item) if len(result) > 1: result = [] result.append({'district': None, 'sub_district': None, 'union': None}) self.cleanAddress = input_address print(result) print(self.execute_time) return result