如何编写一个程序,在字符串列表中返回超过10个字符的字符串数

时间:2015-04-09 03:30:31

标签: python

编写一个程序,在字符串列表中返回超过10个字符的字符串数。

我的节目:

def count(List):
    if len(List) > 10:
        x = "".count(List)

    if len(List) <= 10:
        x = "None"

    return x

def main():
    Listy = input("Please enter a list of strings: ")
    s = []
    for i in Listy:
        Split = i.replace("[","").replace('"','').replace("]","").split(",")
        s += Split

    y = count(s)
    print(y)

main()

我编写了这个程序,但似乎count()函数存在问题。我不确定原因。

4 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

def check_long(lst):
    return len([item for item in lst if len(item) > 10])

这将创建一个过滤列表,其中只包含超过10个字符的项目,然后返回上述列表的长度。

答案 1 :(得分:0)

您可以尝试使用此代码,这将返回给定字符串列表的计数列表。

def count(strings):
    if len(strings) > 10:
        x = len(strings)

    if len(strings) <= 10:
        x = "None"

    return x

def main():
    List = input("Please enter a list of strings: ")
    y = []
    for i in List:
        y.append(count(i))

    print(y)

main()

输出:[16,20,&#39;无&#39;,15]

希望这会对你有所帮助。

答案 2 :(得分:0)

这将为您提供列表中有多少个单词超过10个字符:

注意:我使用raw_input因为我在Python 3中使用Python 2.7使用input,用户也必须输入以逗号和空格分隔的单词

strings = raw_input("Please enter words divided by comma and white space: ")
x = strings.split()
y = []
for i in x:
    if len(i) > 10:
        y.append(i) 
print len(y)

答案 3 :(得分:0)

也许这有助于你

<强>已更新

def count(List):
  if len(List) > 10:
      x = len(List)
  else:
      x = "None"
  return x

def main():
  Listy = input("Please enter a list of strings: ").split(",")
  for index in Listy:
      y = count(index)
      print(y)

main()的

如果输入为:

lol, llllllllllllllllllll, rfssfsfsdf

输出应为:

None
21
11