I have an section id A00-A09. Anything like A01, A01.01, A02 till A09.09 should be
classified under this section id. How can i do this in Python? At the moment I can only match string with exact character.
4 Answers
Cut the section id and compare:
sid = "A00-A09"
def under_sid(ssid, sid):
sid_start, sid_end = sid.split("-")
return ssid[:3] >= sid_start and ssid[:3] <= sid_end
for i in ["A01", "A01.01", "A02", "A09.09"]:
assert under_sid(i, sid)
for i in ["B01", "A22.01", "A93", "A19.09"]:
assert not under_sid(i, sid)
Comments
You can do partial matches using startswith() and endswith(). Assuming the full id is always in a X12.Y34 - each part is a letter and two numbers, separated by . or - (or any character):
>>> id = 'A03.A07'
>>> section_id = id[:3]
>>> section_id
'A03'
>>> id.startswith('A03')
True
>>> id.startswith('A07')
False # so won't match with the subsection.
>>> sub_section_id = id[-3:]
>>> sub_section_id
'A07'
And you can convert it to uppercase if the input can sometimes be lowercase.
remodule, regexremodule?