Python 计算字符串长度为2或更多且第一个和最后一个字符相同的字符串数

Python 计算字符串长度为2或更多且第一个和最后一个字符相同的字符串数,python,Python,示例列表:['abc'、'xyz'、'aba'、'1221'] 预期结果:2 def match_words(words): ctr = 0 for word in words: if len(word) > 1 and word[0] == word[-1]: ctr += 1 return ctr print(match_words(['abc', 'xyz', 'aba', '1221'])) 在ls中存储列表值 ls= ['abc', 'xyz

示例列表:
['abc'、'xyz'、'aba'、'1221']

预期结果:2

def match_words(words):
  ctr = 0

  for word in words:
    if len(word) > 1 and word[0] == word[-1]:
      ctr += 1
  return ctr

print(match_words(['abc', 'xyz', 'aba', '1221']))

在ls中存储列表值

ls= ['abc', 'xyz', 'aba', '1221']   
该代码循环列表的每个项目,并存储满足给定条件的每个列表项目。“len”函数表示输出列表的长度

len([x for x in ls if (len(x) >= 2 and x[-1] == x[0])])    

你的问题是什么?代码似乎有效。你还想要什么\