-1

I have the following function which maps the first two letters of a postcode to a country. However, it does not recognise when there is only one letter followed by a number i.e. "B4" or "N1, so it does not map it and returns "Other".

What could I do to make it recognise single letters followed by numbers?

My function is:

def parse_to_find_country(x):

if (x == "BT"):
   return "Northern Ireland"
elif (x == "CF")|(x == "SA")|(x == "LD")|(x == "LL")|(x == "NP"):
   return "Wales"
elif (x == "AB")|(x == "DD")|(x == "DG")|(x == "EH")|(x == "FK")|(x == "G")|(x == "HS")|(x == "IV")|(x == "KA")|(x == "KN")|(x == "KY")|(x == "ML")|(x == "PA")|(x == "PH")|(x == "TD")|(x == "ZE"):
   return "Scotland"
elif (x == "AL")|(x == "B")|(x == "BA")|(x == "BB")|(x == "BD")|(x == "BH")|(x == "BL")|(x == "BN")|(x == "BR")|(x == "BS")|(x == "CA")|(x == "CB")|(x == "CH")|(x == "CM")|(x == "CO")|(x == "CR")|(x == "CT")|(x == "CV")|(x == "CN")|(x == "DA")|(x == "DE")|(x == "DH")|(x == "DL")|(x == "DN")|(x == "DT")|(x == "DY")|(x == "E")|(x == "EC")|(x == "EH")|(x == "EN")|(x == "EX")|(x == "FY")|(x == "GL")|(x == "GU")|(x == "HA")|(x == "HD")|(x == "HG")|(x == "HP")|(x == "HR")|(x == "HU")|(x == "HX")|(x == "IG")|(x == "IP")|(x == "KT")|(x == "L")|(x == "LA")|(x == "LE")|(x == "LN")|(x == "LS")|(x == "LU")|(x == "M")|(x == "ME")|(x == "M")|(x == "MK")|(x == "N")|(x == "NE")|(x == "NG")|(x == "NN")|(x == "NR")|(x == "NW")|(x == "OL")|(x == "OX")|(x == "PE")|(x == "PL")|(x == "PO")|(x == "PR")|(x == "RG")|(x == "RH")|(x == "RM")|(x == "S")|(x == "SE")|(x == "SG")|(x == "SK")|(x == "SL")|(x == "SM")|(x == "SN")|(x == "SO")|(x == "SP")|(x == "SR")|(x == "SS")|(x == "ST")|(x == "SW")|(x == "SY")|(x == "TA")|(x == "TF")|(x == "TN")|(x == "TQ")|(x == "TR")|(x == "TS")|(x == "TW")|(x == "UB")|(x == "W")|(x == "WA")|(x == "WC")|(x == "WD")|(x == "WF")|(x == "WN")|(x == "WR")|(x == "WS")|(x == "WV")|(x == "YO"):
   return "England"
else:
   return "Other"
Hector
  • 17
  • 5
  • Your code looks to be easily translated to a dictionary: ```python ``` – Reda Drissi Mar 04 '21 at 11:24
  • Okay, so you want to detect when the string *starts with* some particular thing? What happened when you put `python string starts with` into a search engine? – Karl Knechtel Mar 04 '21 at 11:26
  • shorter: `if x in ("CF", "SA", "LD", "LL", "NP")`. But if you want to check if string starts with `B` then you should rather use `x.startswith("B")`. Eventually you could use modue `re` to use `regex` like `B\d` to check char `B` with any `digit` – furas Mar 04 '21 at 14:55
  • `|` is for bit operations - for booleans (True/False) you should use `or` – furas Mar 04 '21 at 14:56

2 Answers2

0

Add this before the if block:

if x[1:].isnumeric():
    x = x[0]

It checks if the second (and next) characters are a number and in that case removes it, making the if clause work.

frab
  • 1,162
  • 1
  • 4
  • 14
0

Remove numbers from the string

from string import digits
remove_digits = str.maketrans('', '', digits)
x = x.translate(remove_digits)
Alex Metsai
  • 1,837
  • 5
  • 12
  • 24