Click here to Skip to main content
15,568,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a matching function that looks like the following
def mapping(a):
  if a in ["A1" , "A2" , "A3" , "A4" , "A5" , "A6" , "A7"]:
    return "Avyayıbhava"
  elif a in ["Bs2" , "Bs3" , "Bs4" , "Bs5" , "Bs6" , "Bs7" , "Bsd" , "Bss" , "Bsu" , "Bsp" , "Bsg" , "Bvs" , "BvS" , "Bvp" , "BvU" , "Bsmn" , "Bb","BvP"]:
    return "Bahuvrıhi"
  elif a in ['Di' , "Ds" , "E"]:
    return "Dvandva"
  elif a in ["d" , "S"]:
    return "anya"
  elif a in ["T1" , "T2" , "T3" , "T4" , "T5" , "T6" , "T7" , "Tn" , "Tp" , "Tk" , "Tg" , "Td" , "Tdu" , "Tds" , "U" , "U2" , "U3" , "U4" , "U5" , "U7" , "Tm" , "Tb" , "K1" ,"k1" "K2" , "K3" , "K4" , "K5" , "K6" , "K7" , "Km"]:
    return "Tatpurusa"


The match cases that I have inserted are tags, but they could actually appear in any case format. So Bs2 could appear as BS2 or bS2 or bs2 but it needs to be matched as the same thing. So how do I make the above function case insensitive for the above-given match cases.

What I have tried:

Created the matching function
def mapping(a):
  if a in ["A1" , "A2" , "A3" , "A4" , "A5" , "A6" , "A7"]:
    return "Avyayıbhava"
  elif a in ["Bs2" , "Bs3" , "Bs4" , "Bs5" , "Bs6" , "Bs7" , "Bsd" , "Bss" , "Bsu" , "Bsp" , "Bsg" , "Bvs" , "BvS" , "Bvp" , "BvU" , "Bsmn" , "Bb","BvP"]:
    return "Bahuvrıhi"
  elif a in ['Di' , "Ds" , "E"]:
    return "Dvandva"
  elif a in ["d" , "S"]:
    return "anya"
  elif a in ["T1" , "T2" , "T3" , "T4" , "T5" , "T6" , "T7" , "Tn" , "Tp" , "Tk" , "Tg" , "Td" , "Tdu" , "Tds" , "U" , "U2" , "U3" , "U4" , "U5" , "U7" , "Tm" , "Tb" , "K1" ,"k1" "K2" , "K3" , "K4" , "K5" , "K6" , "K7" , "Km"]:
    return "Tatpurusa"
Posted
Updated 3-Jun-21 3:48am

Change mapping this way:
Python
def mapping(a):
  a = a.upper()
  if a in ["A1" , "A2" , "A3" , "A4" , "A5" , "A6" , "A7"]:
    return "Avyayıbhava"
  elif a in ["BS2" , "BS3" , "BS4" , "BS5" , "BS6" , "BS7" , "BSD" , "BSS" , "BSU" , "BSP" , "BSG" , "BVS", "BVP" , "BVU" , "BSMN" , "BB","BVP"]:
    return "Bahuvrıhi"
# ...

That is change the input string, as well all the list items to upper case (removing the duplicates).
 
Share this answer
 
Use one of the string class methods[^] to change the case as appropriate.
 
Share this answer
 
Quote:
Create case insensitive matching function

Generally speaking, either you have an insensitive comparison operator, either you force the case.
First, uppercase or lowercase is your choice.
Second, change your source to make all list of strings with same case.
Third, force parameter to same case as the strings
Python
def mapping(a):
  a=a.upper() # force parameter to uppercase
  if a in ["A1" , "A2" , "A3" , "A4" , "A5" , "A6" , "A7"]:
    return "Avyayıbhava"
  ...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900