Python基于字符串中的第一个和最后一个字符进行排序

时间:2015-11-07 23:27:48

标签: python-2.7

我在python 2上通过谷歌参加在线初学者课程,我无法弄清楚其中一个问题的答案。在这里,并提前感谢您的帮助!

# 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

我尝试了一些不同的东西。这就是我上次尝试时离开的地方,并决定寻求帮助。我花了更多的时间来思考这个,而不是我提到

1 个答案:

答案 0 :(得分: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']))