Skip to main content
Commonmark migration
Source Link

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'.

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'.

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'.

indented; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

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

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

Your help is much appreciated. Here is my code:

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))

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

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

Your help is much appreciated. Here is my code:

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))

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

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))
added 18 characters in body
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

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"

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

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

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

Examples :

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

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

Examples :

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

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

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

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

'city' is HP than 'county',

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

'county' is HP than 'state'

'city' is HP than 'county',

and 'state' is HP than 'country'.

'county' is HP than 'state'

and 'state' is HP than 'country'.

I have written a code for that but BEING NEW TO PYTHONbeing new to Python,

  the code looks CLUNKYclunky. Is there any shorter method  ?

Your help is much appreciated. Here Isis my code  :

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 piority(hp) than 'city'.

'city' is HP than 'county',

'county' is HP than 'state'

and 'state' is HP than 'country'.

I have written a code for that but BEING NEW TO PYTHON,

  the code looks CLUNKY. Is there any shorter method  ?

Your help is much appreciated. Here Is my code  :

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 piority(hp) than 'city'.

'city' is HP than 'county',

'county' is HP than 'state'

and 'state' is HP than 'country'.

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

Your help is much appreciated. Here is my code:

Source Link
SanBan
  • 195
  • 3
Loading