I need to compare a string against 2 list. I have the following list:
labels = ['admin', 'Admin', 'staff', 'techAdmin', 'techadmin']
accessCodeList = ['0x000', '0xAaA']
String Inputs:
mystring1 = "admin 0x000"
mystring2 = "demo 0x000"
mystring3 = "techAdmin 0x00"
mystring4 = "tech 0xAaA"
mystring5 = "techAdmin 0xAaA"
mystring6 = "Staff 0x000" #-- starts with Uppercase
Outputs:
mystring1 = "Found"
mystring2 = "Not Found"
mystring3 = "Not Found"
mystring4 = "Not Found"
mystring5 = "Found"
mystring6 = "Found" #-- case insensitive search result
CodePudding user response:
Try converting labels
and access_codes
into a set for O(1)
lookup time:
>>> labels = ['admin', 'Admin', 'staff', 'techAdmin', 'techadmin']
>>> access_codes = ['0x000', '0xAaA']
>>> inputs = ['admin 0x000', 'demo 0x000', 'techAdmin 0x00', 'tech 0xAaA', 'techAdmin 0xAaA', 'Staff 0x000']
>>> labels = {l.lower() for l in labels}
>>> access_codes = set(access_codes)
>>> for s in inputs:
... label, code = s.split()
... if label.lower() in labels and code in access_codes:
... print(f'{s}, Found')
... else:
... print(f'{s}, Not Found')
...
admin 0x000, Found
demo 0x000, Not Found
techAdmin 0x00, Not Found # 0x00 != 0x000 I think this is a typo in your example?
tech 0xAaA, Not Found
techAdmin 0xAaA, Found
Staff 0x000, Found
CodePudding user response:
Convert your input strings to lowercase and then check if the other lists contain the words or not. :
labels = ['admin', 'staff', 'techadmin']
accessCodeList = ['0x000', '0xAaA']
mystring6 = "Staff 0x000"
mystring6_split = mystring6.split()
mystring6_split[0] = mystring6_split[0].lower()
if mystring6_split[0] in labels and mystring6_split[1] in accessCodeList:
print('Found')
else:
print('Not Found')
CodePudding user response:
Use casefold
.
labels = ['admin', 'staff', 'techadmin']
accessCodeList = ['0x000', '0xAaA']
mystring1 = "admin 0x000"
mystring2 = "demo 0x000"
mystring3 = "techAdmin 0x00"
mystring4 = "tech 0xAaA"
mystring5 = "techAdmin 0xAaA"
mystring6 = "Staff 0x000" #-- starts with Uppercase
label, access_code = mystring1.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
label, access_code = mystring2.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
label, access_code = mystring3.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
label, access_code = mystring4.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
label, access_code = mystring5.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
label, access_code = mystring6.split()
print('Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found')
and followind the DRY principle, you could create a function.
def find_in_str(mystr):
label, access_code = mystr.split()
return 'Found' if label.casefold() in labels and access_code in accessCodeList else 'Not Found'
print(find_in_str(mystring1))
print(find_in_str(mystring2))
print(find_in_str(mystring3))
print(find_in_str(mystring4))
print(find_in_str(mystring5))
print(find_in_str(mystring6))