使用endswith()的替代解决方案?

时间:2019-06-06 04:12:23

标签: python python-3.x

我有解决此问题的方法,但我不想使用endswith()方法,如何使用“ str [i:j]提示”解决该问题

给出两个字符串,如果两个字符串中的任何一个出现在另一个字符串的末尾,则返回True,而忽略大小写的差异(换句话说,计算不应该区分大小写)。注意:s.lower()返回字符串的小写版本。

直接解决方案:

return a.endswith(b) or a.endswith(b)

5 个答案:

答案 0 :(得分:1)

您可以断言,如果您从每个字符串的末尾开始并逐个字符地进行匹配,则至少其中一个字符串将被用尽而没有任何区别:

def either_endswith(a, b):
    return all(a_char == b_char for a_char, b_char in zip(reversed(a), reversed(b)))

def either_endswith_case_insensitive(a, b):
    return either_endswith(a.lower(), b.lower())

# Positive
either_endswith_case_insensitive('aaaa', 'a')
either_endswith_case_insensitive('a', 'aaaa')
either_endswith_case_insensitive('aaaa', 'A')
either_endswith_case_insensitive('BbbB', 'BBbb')

# Negative
either_endswith_case_insensitive('aaaa', 'c')
either_endswith_case_insensitive('a', 'ccccc')

答案 1 :(得分:1)

def end_other(a, b):
  if a[-len(b):].lower()==b.lower() or b[-len(a):].lower()==a.lower():
    return True
  return False

答案 2 :(得分:0)

正则表达式

import re

# watch out if `b` contains escapable chars
# like \/ or ()
def test_if_a_ends_with_b(a, b):
    return re.search(r'{}$'.format(re.escape(b)), a, re.IGNORECASE)

未导入的香草Python

def test_if_a_ends_with_b(a, b):
    return a.lower().endswith(b.lower())

# or
def test_if_a_ends_with_b(a, b):
    return a[-len(b):].lower() == b.lower()

答案 3 :(得分:0)


        'Declare word object variables
    Dim WordApp     As Word.Application
    Dim WordDoc     As Word.Document
    
        'Declare excel Object variable
    Dim WrkSht      As Worksheet
    Dim Chrt        As ChartObject
    Dim Cht_Sht     As Chart
    Dim wkBk        As Workbook
    
    
    'Optimize Code
  Application.ScreenUpdating = False
  Application.EnableEvents = False
    
        'Set the link to the location where the excel evaluation sheet is located, include file name in the link
   Const Utvärdering As String = "C:\Users\A561004\OneDrive - AF\Desktop\Test\Utvärdering.xlsx"
    
        'Open Excel Utvärdering...
    Application.StatusBar = "Utvärdering"
    Set wkBk = Workbooks.Open(Utvärdering)
    
        ' Select sheet based on name
    Sheets(1).Select
         
            
        'Create a new instance of Word
    Set WordApp = New Word.Application
        WordApp.Visible = True
        WordApp.Activate
        
        
        'Create a new word document
    Set WordDoc = WordApp.Documents.Add
            
            
        'Start a loop
        For Each WrkSht In Sheets
        'WrkSht.ChartObjects.Select
        
       If ActiveSheet.ChartObjects.Count > 0 Then
        
        For Each Cht_Sht In wkBk.Sheets(1).ChartObjects
            Cht_Sht.ChartArea.ChartArea.Copy
        
        'ActiveChart.ChartArea.Select
        'ActiveChart.ChartArea.Copy
        
            With Word.Application.Selection
       .PasteSpecial Link:=False, DataType:=15
       
           WordApp.ActiveDocument.Selections.Add
        'Go to new page
    WordApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext
        'Clear Clipboard
    Application.CutCopyMode = False
       
     End With
     
     Next Cht_Sht
    
        
    Else
        WrkSht.Next.Activate
    End If
        
        'Test loop
        'For each Cht_Sht in 2 To Sheets(ActiveWorkbook.Sheets.Count - 1)
        
     
    
        'Create a Reference to the chart you want to Export
    'ActiveChart.ChartArea.Select
    'On Error Resume Next
    'ActiveChart.ChartArea.Copy
    
    
    
        
        'Paus application 2 sek
    Application.Wait Now + #12:00:02 AM#
        
        
        'Paste into WOrd Document
    'With Word.Application.Selection
     '  .PasteSpecial Link:=False, DataType:=15
       
    ' End With
    
        'New word page Problems here, need to set a new marker in the document for next paste
   ' WordApp.ActiveDocument.Selections.Add
        'Go to new page
  '  WordApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext
        'Clear Clipboard
  ' Application.CutCopyMode = False
    
        'End loop, or start next rotation of loop
        Next WrkSht
        
        'Optimise Code
    Application.EnableEvents = True
    
    On Error GoTo 0
    
End Sub

答案 4 :(得分:0)

def end_other(a, b):
  
  #converting strings to lower case
  a = a.lower()
  b = b.lower()
  
  if a.endswith(b) or b.endswith(a):
    return True
  else:
    return False
      

def end_other(a, b):
  
  #converting strings to lower case
  a = a.lower()
  b = b.lower()
  
  if a[-len(b):] == b or b[-len(a):] == a:
    return True
  return False