2015-10-20 115 views
1

展望放在一起,检查以下Python字符串函数

给定两个字符串一个简单的Python函数返回True,如果任一字符串出现在另一个字符串, 的尽头忽略大/小写的差异(换句话说,计算不应该是“敏感的情况下 ”)。

end_other('Hiabc', 'abc') → True 
end_other('AbC', 'HiaBc') → True 
end_other('abc', 'abXabc') → True 

回答

4

def end_other(s1, s2): 
    s1 = s1.lower() 
    s2 = s2.lower() 
    return s1.endswith(s2) or s2.endswith(s1) 
+0

不错的使用'或' – The6thSense

+0

感谢。现在在正确的轨道上。我明白,人们可能会认为我只是在寻找答案,但事实并非如此。即时通讯新到这一点,当你们可以在几分钟内帮助我学习同样的东西时,不用花一整天的时间注视屏幕。然后我会用你给我未来工作的东西。 – johndoe12345

+0

上面的函数仍然返回true,即使我添加一个字符串,应该返回false?我已经添加打印end_other(“不不不,NONON”)和我仍然得到真正 – johndoe12345

0

您可以使用切片。

>>> s1 = "Hiabc" 
>>> s2 = "abc" 
>>> l = len(s2) 
>>> s2 == s1[-l:] 
True 
>>> s2 = "aBc" 
>>> s2 == s1[-l:] 
False 

你可以阅读更多关于字符串和切片here

切片意味着根据通过的index获取部分字符串。一般建筑看起来像这样:string[starting_index:ending_index]

让我们来看看这个:

string1 = "doggy" string1[0:1] -> "d" string1[0:2] -> "do" string1[0:] = string[:] = "doggy" (from zero to end)

您还可以通过指数较小比0:

string1[-1:] (from -1 to end) -> "y" 所以在上面的代码中调用s1[-l:]意味着起始位置是s2长度sentece。在这种情况下,s1[-l:] = 'abc' = s1[-3:]。由于s2 lenght为3

+0

上述切断方法意味着S1将永远是Hiabc,如果我没有? str.endswith听起来更好?我怎样才能使str1的第一个字符串的值和str2的第二个值,?我不能硬编码这些COS,他们是不同的每一行。 – johndoe12345

+0

@ johndoe12345我的答案显示你应遵循的路径。你应该去问问谷歌如何在python中定义'def'ine函数,并尝试自己解决其他问题。 – Laszlowaty

+0

你能解释这里的代码吗? – johndoe12345

1

试试你可以使用正则表达式

def end_other(s1,s2): 
    return bool(re.search(s1+'$',s2,re.I)) or bool(re.search(s2+'$',s1,re.I)) 
+0

它是否有效? – Pynchia

+0

哎呀对不起固定:) –

1

或者,如果你需要 “创造” 自己的函数:

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",它将检查字符的与string12string21string1

+0

网络礼仪设想援引以前的答案,如果反复在你 – Pynchia

+0

只需编辑它,我开始写的答案之前,任何其他方面都没有看到它@Pynchia –

+0

你能explan这个代码吗?我不明白的if语句和语法有 – johndoe12345

1

3下面是使用拉链另一种方法中,这避免了必须做的endswith()re.search()或切片多次通过。它可反向重复2个字符串,如果所有的字母都等于到字符串之一用尽返回true:

def end_other(s1, s2): 
    return all(a == b for a, b in zip(reversed(s1.lower()), reversed(s2.lower()))) 

在Python2你可以使用itertools.izip()的边际空间的改进:

>>> end_other('Hiabc', 'abc') 
True 
>>> end_other('AbC', 'HiaBc') 
True 
>>> end_other('abc', 'abXabc') 
True 
>>> end_other('cbc', 'abXabc') 
False 
0

你可以使用OOP,如果你想,继承str,写适当的方法:

>>> class String(str): 
...  def isend(self, other): 
...    self = self.lower() 
...    other = other.lower() 
...    return self.endswith(other) or other.endswith(self) 
... 
>>> a = String('HiAbC') 
>>> b = String('Bc') 
>>> a.isend(b) 
True 
>>> b.isend(a) 
True 
>>> c = String('Hello') 
>>> d = String('el') 
>>> c.isend(d) 
False 
>>> d.isend(c) 
False