【发布时间】:2019-03-11 23:36:50
【问题描述】:
编写一个程序,接受:
s1 逗号分隔的字符串 s2 要计数的字符串 并打印第二个字符串在第一个字符串的逗号分隔成员中出现的次数。
例如,如果用户输入一、二、一、三然后是一,您的程序应该打印 2。提示:您会发现方法 list.count() 在这里很有帮助。您应该使用的 input() 语句和相关的字符串处理已在下面的示例代码中提供。 (注意:假设用户输入的系列中的每个逗号后面都有一个空格。)
这是我目前所拥有的:
# split on comma + space to create the list
s1= input('Please enter a series of comma-separated strings: ')
# split on comma + space to create the list
1 = s1.split(', ')
# input the string to count in the list
s2 = input('Please enter a string to count: ')
# print out the number of times s2 occurs in s1
print(list.count(s2))
我得到了一个他们正在寻找的示例,但仍然没有完全理解这个概念。这是他们给我的:“例如,如果用户输入一、二、一、三,然后是一,您的程序应该打印 2。提示:您会发现方法 list.count() 在这里很有帮助。”
【问题讨论】:
标签: python-3.x string list