5.7 题解
2019-04-02 本文已影响0人
反复练习的阿离很笨吧
import collections
import copy
s = "我/是/一个/测试/句子/,/大家/赶快/来/统计/我/吧/,/大家/赶快/来/统计/我/吧/,/大家/赶快/来/统计/我/吧/,/重要/事情/说/三遍/!/"
s_list = s.split('/')
# 为避免迭代时修改迭代对象本身,创建一个列表的深拷贝,也可用浅拷贝s_list_backup = s_list[:]
s_list_backup = copy.deepcopy(s_list)
[s_list.remove(item) for item in s_list_backup if item in ',。!”“']
collections.Counter(s_list)
Counter({'我': 4,
'是': 1,
'一个': 1,
'测试': 1,
'句子': 1,
'大家': 3,
'赶快': 3,
'来': 3,
'统计': 3,
'吧': 3,
'重要': 1,
'事情': 1,
'说': 1,
'三遍': 1})
上面已经把标点符号单独用/分出来了,但是在英文中,标点符号和前一个单词之间没有空格,就不能现split再去标点,需要先去标点再split成列表。
s = "Not clumsy person in this world, only lazy people, only people can not hold out until the last."
s = s.lower()
a = ''.join(c for c in s if c not in ',.')
s_list = a.split(' ')
这个在本地的答案都是对的,但是交到oj上就报非0异常。
import collections
def countfeq(s):
a = ''.join(c for c in s if c not in ',.')
s_list = a.split(' ')
return collections.Counter(s_list)
if __name__ == "__main__":
s = "Not clumsy person in this world, only lazy people, only people can not hold out until the last."
s_dict = countfeq(s.lower())
word = input()
print(s_dict[word])