Python 2.7 基于字符串中第一个和最后一个字符的Python排序
我正在通过谷歌学习python 2的在线初学者课程,我想不出其中一个问题的答案。给你,谢谢你的帮助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
# 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']))