3
\$\begingroup\$

Requirement:

Python Function will read a LIST of TUPLES. The structure of the tuple is ("STRING", "STRING","STRING",INT,"GEOGRAPHIC VALUE")

This Geographic value can have 5 values "zip", "city", "county", "state" and "country"

Now if a Tuple has elements where geographic values are (....,"zip"),(....,"county"), (....,"country") then function should return the element/tuple which contains "zip"

Examples :

Input : [('aa', 'bb', 'cc', 1, 'state'), ('aa', 'bb', 'cc', 2, 'zip')]

Output : ['aa', 'bb', 'cc', 2, 'zip']

In other words 'zip' is higher priority(hp) than 'city'.

'city' is HP than 'county',

'county' is HP than 'state'

and 'state' is HP than 'country'.

I have written code for that but being new to Python, the code looks clunky. Is there any shorter method?

def unique_list(input):
 my_list = input
 print(my_list)
 last_list = []
 zipcnt = -1
 citicnt = -1
 countycnt = -1
 statecnt = -1
 return_list_zip = []
 return_list_city = []
 return_list_county = []
 return_list_state = []
 return_list_country = []
 for j in range(len(my_list)):
 if(my_list[j][4]) == "zip":
 zipcnt = len(my_list)
 return_list_zip = list(my_list[j])
 continue
 elif (my_list[j][4] == 'city' and zipcnt == -1):
 citycnt = len(my_list)
 return_list_city = list(my_list[j])
 continue
 
 elif (my_list[j][4] == 'county' and zipcnt == -1 and citicnt == -1):
 countycnt = len(my_list)
 return_list_county = list(my_list[j])
 continue
 elif (my_list[j][4] == 'state'and zipcnt == -1 and citicnt == -1 and countycnt == -1):
 statecnt = len(my_list)
 return_list_state = list(my_list[j])
 continue
 elif (my_list[j][4] == 'country'and zipcnt == -1 and citicnt == -1 and countycnt == -1 and statecnt == -1):
 return_list_country = list(my_list[j])
 continue
 if(zipcnt != -1):
 return_list = return_list_zip
 elif(citycnt != -1):
 return_list = return_list_city
 elif(countycnt != -1):
 return_list = return_list_county
 elif (statecnt != -1):
 return_list = return_list_state
 else:
 return_list = return_list_country
 return return_list
input_list = [ ("aa", "bb", "cc", 1,"state"), ("aa","bb","cc",2,"zip")]
input_list1 = [("XX","YY","NN",1,"country"),("XX","YY","NN",2,"city"), ("XX","YY","NN",3,"state")]
input_list2 = [("NN","FF","PP",10,"state"),("NN","FF","PP",11,"country"),("NN","FF","PP",10,"city")]
print(unique_list(input_list))
print(unique_list(input_list1))
print(unique_list(input_list2))
asked Apr 15, 2016 at 19:28
\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

You're right, it is clunky.

The first important step is to pick a function name that succinctly describes the task that it performs, namely, picking the most specific address component.

Once you do that, you might realize that "the most X of Y" means that you should use the max() function. You just have to map each tuple to some quantitative measure of "specificity".

def most_specific_address(addresses):
 SPECIFICITY = {
 'country': 1, 'state': 2, 'county': 3, 'city': 4, 'zip': 5,
 }
 return max(addresses, key=lambda addr: SPECIFICITY[addr[4]])
answered Apr 15, 2016 at 20:34
\$\endgroup\$
2
  • \$\begingroup\$ I read about using key = lambda using following link. stackoverflow.com/questions/18296755/… Just one question: What does Lamda return as 'key' is it the tuple having highest specificity ? \$\endgroup\$ Commented Apr 15, 2016 at 22:08
  • 1
    \$\begingroup\$ For any input tuple, the lambda maps it to a specificity level. Then the max() function returns the tuple whose specificity level is the greatest. \$\endgroup\$ Commented Apr 15, 2016 at 23:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.