我的字典键需要这种格式的“/ cat/”,但我不断收到多个正斜杠。这是我的代码:
# Defining the Digraph method #
def digraphs(s):
dictionary = {}
count = 0;
while count <= len(s):
string = s[count:count + 2]
count += 1
dictionary[string] = s.count(string)
for entry in dictionary:
dictionary['/' + entry + '/'] = dictionary[entry]
del dictionary[entry]
print(dictionary)
#--End of the Digraph Method---#
这是我的输出:
我这样做:
digraphs('我的猫在帽子里')
{'///in///': 1, '/// t///': 1, '/// c///': 1, '//s //': 1, '/my/': 1, '/n /': 1, '/e /': 1, '/ h/': 1, '////ha////': 1, '//////': 21, '/is/': 1, '///ca///': 1, '/he/': 1, '//th//': 1, '/t/': 3, '//at//': 2, '/t /': 1, '////y ////': 1, '/// i///': 2}