Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 基于字符串中第一个和最后一个字符的Python排序_Python 2.7 - Fatal编程技术网

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

Python 2.7 基于字符串中第一个和最后一个字符的Python排序,python-2.7,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 do

我正在通过谷歌学习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
我尝试了一些不同的东西。这正是我上次尝试的地方,我决定寻求帮助。我花了更多的时间思考这个问题,我不想提及

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']))