希望将检查以下内容的简单python函数组合在一起
给定两个字符串时,如果其中一个字符串出现在另一个字符串的末尾,则返回True,忽略大小写差异(换句话说,计算不应该“区分大小写”)。
end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True发布于 2015-10-20 07:42:51
试着
def end_other(s1, s2):
s1 = s1.lower()
s2 = s2.lower()
return s1.endswith(s2) or s2.endswith(s1)发布于 2015-10-20 07:49:40
您可以使用regex
def end_other(s1,s2):
return bool(re.search(s1+'$',s2,re.I)) or bool(re.search(s2+'$',s1,re.I))发布于 2015-10-20 07:57:36
或者如果您需要“创建”您自己的函数:
def check(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
check = True
# If string 1 is bigger then string 2:
if( len(str1) > len(str2) ):
# For each character of string 2
for i in range(len(str2)):
# Compare the character i of string 2 with the character at the end of string 1, keeping the order
if str2[i] != str1[-(len(str2)-i)]:
check = False
# If string 2 is bigger then string 1:
else:
# For each character of string 1
for i in range(len(str1)):
# Compare the character i of string 1 with the character at the end of string 2, keeping the order
if str1[i] != str2[-(len(str1)-i)]:
check = False
return check因此,基本上,如果是string1 = "ABCD"和string2 = "CD",它将使用string1的2检查string2的字符0,用string1的3检查string2的1 >
https://stackoverflow.com/questions/33230673
复制相似问题