2015-11-07 133 views
0

我在Python 2上通過谷歌在線初學者課程,我無法弄清楚其中一個問題的答案。在這裏,並提前感謝您的幫助!基於字符串中的第一個字符和最後一個字符的Python排序

# A. match_ends 
# Given a list of strings, return the count of the number of 
# strings where the string length is 2 or more and the first 
# and last chars of the string are the same. 
# Note: python does not have a ++ operator, but += works. 

def match_ends(words): 
    a = [] 
    for b in words: 

return 

我嘗試了幾個不同的東西。這就是我最後一次嘗試的時候,並決定尋求幫助。我花了更多的時間思考這個問題比我還在提

回答

0
def match_ends(words): 
    a = [] 
    for b in words: 
     if (len(b) > 2 and b[0] == b[len(b)-1]): 
      a.append(b) 
    return a 


def match_ends2(words): 
    return [x for x in words if len(x) > 2 and x[0] == x[len(x)-1]] 

print(match_ends(['peter','paul','mary','tibet'])) 
print(match_ends2(['peter','paul','mary','tibet'])) 
+0

非常感謝你 – montanazach

相關問題