我正在处理的问题是,输入由许多测试用例,单词号和单词x测试用例组成。
例如:
2
3
Potato
Spud
Baked
2
Fried
Chicken
您如何在测试用例之间进行划分?
我不能只使用空格或特定的数字,所以有没有像正则表达式或类似的东西?
msg = [string(msg) for msg in input().split()] ]
for i in msg:
count = str.count('c') + str.count('o') + str.count('d')
我应该能够看到字母c o d出现的次数。
答案 0 :(得分:0)
编辑: 首先,您应该获取input()并使用while循环或其他方式存储此输入,然后处理该输入文本,例如:
text = """
2
3
Potato
Spud
Baked
2
Fried
Chicken
"""
msg = text.split()
print(msg)
for i in msg:
count = i.count('c') + i.count('o') + i.count('d')
print('count for', i, '=', count)
输出:
['2', '3', 'Potato', 'Spud', 'Baked', '2', 'Fried', 'Chicken']
count for 2 = 0
count for 3 = 0
count for Potato = 2
count for Spud = 1
count for Baked = 1
count for 2 = 0
count for Fried = 1
count for Chicken = 1