patternpythonMinor
Python Find Tuple by Geographic Priority
Viewed 0 times
prioritytuplepythonfindgeographic
Problem
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 :
Output :
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(
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(
Solution
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
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]])Code Snippets
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]])Context
StackExchange Code Review Q#125810, answer score: 4
Revisions (0)
No revisions yet.