2015-11-07 5 views
0

Im, берущий курс начинающего онлайн через google на 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

Смежные вопросы