python compare subsection of 2 strings and see if they match
I have 2 strings e.g.
str1 = 'section1.1: this is a heading for section 1'
and
str2 = 'section1.1: this is a heading for section 1.1'
I want to compare the text which comes after 'section1.1:' and return whether it is the same or not. In the example it would return false as the first says section 1
and the second says section 1.1
The fi开发者_运维百科rst piece of the string can be anything e.g. subsection2.5: but always ends with a :
What is the best way to do this using Python?
Use the split method of the strings to split on only the first :
:
>>> str1 = 'section1.1: this is a heading for section 1'
>>> str2 = 'section1.1: this is a heading for section 1.1'
>>> str1.split(':', 1)[1]
' this is a heading for section 1'
>>> str2.split(':', 1)[1]
' this is a heading for section 1.1'
Depending on how well you know what the format will be, you might do something like this.
In [1]: str1 = 'section1.1: this is a heading for section 1'
In [2]: str2 = 'section1.1: this is a heading for section 1.1'
In [3]: if str1.split(":", 1)[1] == str2.split(":", 1)[1]:
...: print "true"
In [4]: str2 = 'section1.1: this is a heading for section 1'
In [7]: if str1.split(":", 1)[1] == str2.split(":", 1)[1]:
...: print "true"
true
You can always strip the responses if you're concerned about trailing or leading whitespace to be different.
(edit: Missing line in IPython session)
The split(":")
solutions in the other answers work fine. If you already parsed the first part (before the colon) in some way, and you know the length, you could do a simple string comparison like this:
parsedUntilColon = "section1.1:" # for example
if str1[len(parsedUntilColon):] == str2[len(parsedUntilColon):]:
print "strings are equal"
You can translate this pretty directly into code:
def compare( str1, str2):
# I want to compare the text which comes after the first ':' (for example)
def find_that_part( s ):
idx = s.find(':') # where is the first ':'?
return s[idx:] # the part after it
# and return whether it is the same or not.
return find_that_part(str1) == find_that_part(str2)
A one liner should be cool!!
>>> str1 = 'section1.1: this is a heading for section 1'
>>> str2 = 'section1.1: this is a heading for section 1.1'
>>> cmp = lambda str1, str2: [x_.findall(str1) == x_.findall(str2) for x_ in [re.compile(':[\w*\s*\.*]*')]][0] and True
>>> cmp(str1, str2)
False
精彩评论