【问题标题】:how to Write a function that takes a list of strings as input, and returns the count of the number of strings [closed]如何编写一个将字符串列表作为输入的函数,并返回字符串数的计数[关闭]
【发布时间】:2014-01-09 03:16:02
【问题描述】:

字符串长度应为 2 或更多,并且字符串的第一个字符和最后一个字符应相同。

我正在尝试编写一个函数,该函数将字符串列表作为输入,并返回字符串长度应为 2 或更多且字符串的第一个和最后一个字符应相同的字符串数的计数.

【问题讨论】:

  • 请告诉我们你做了什么以及你遇到的错误
  • 听起来像是家庭作业。

标签: python string list python-2.7 char


【解决方案1】:

我会做以下事情:

def special_strings_count(strings):
    return len([x for x in strings if len(x) > 1 and x[0] == x[-1]])

strings = [] # your list of strings
print special_strings_count(strings)

【讨论】:

  • 成功了。谢谢@Marcb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 2022-11-15
  • 2013-10-04
  • 2018-11-18
  • 1970-01-01
相关资源
最近更新 更多