以match_结尾的python程序

以match_结尾的python程序,python,Python,你们认为我做错了什么?我应该如何改进 结果如下: # 5.- 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 ++ operato

你们认为我做错了什么?我应该如何改进

结果如下:

# 5.- 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.
#       For example, the result of n = n + 1 can be also achieved by n += 1.



def match_ends(words):
  # +++your code here+++
  if len(words)>=2:
    return words[0]and words[-1:]==words[-1:]
    words+=words
这应该适合您:)

如果你想让它更像蟒蛇,你可以:

def match_ends(words):
    word_count=len(words)
    results=[]
    for x in words:
        if len(x)>2 and x[0]==x[len(x)-1]:
            results.append(x)
    return word_count,results

word_list=["hello","wow"]
matched_words=match_ends(word_list)
print matched_words

更精简的方法:

def match_ends(words):
    word_count=len(words)
    results=[x for x in word_list if len(x)>2 and x[0]==x[len(x)-1]]
    return word_count,results

听起来像是家庭作业,而且大部分代码都是错的。。例如,你认为
len(words)
做什么?什么是单词[0]?注意:这里有一个交互式shell,您可以尝试其中的大部分内容
def match_ends(words):
    word_count=len(words)
    results=[x for x in word_list if len(x)>2 and x[0]==x[len(x)-1]]
    return word_count,results
def match_ends(words):
  count = 0
  for i in words:
    if len(i) >= 2 and i[-1] == i[0]:
      count = count + 1
  return count