基于字符串中的第一个字符和最后一个字符的Python排序
问题描述:
我在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
我尝试了几个不同的东西。这就是我最后一次尝试的时候,并决定寻求帮助。我花了更多的时间思考这个问题比我还在提
答
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']))
非常感谢你 – montanazach