像一個例子:獲得最後三個字母在字典蟒蛇
text = ' "west best worst first tapping snapping in a pest the straining
singing forest living'
a_dict = get_last_three_letters_dict(text)
remove_less_than_2(a_dict)
print("1.")
print_dict_in_key_order(a_dict)
text = 'Kublai Khan does not necessarily believe everything Marco Polo says
when he describes the cities visited on his expeditions but the emperor of
the Tartars does continue listening to the young Venetian with greater
attention and curiosity than he shows any other messenger or explorer of his'
a_dict = get_last_three_letters_dict(text)
remove_less_than_2(a_dict)
print("2.")
print_dict_in_key_order(a_dict)
我試圖將字符串成小寫轉換,然後返回,其具有的任何單詞的最後三個字母鍵的字典對象在長度大於2的文本串中,以及相應的值,即以這些最後三個字母結尾的文本參數串中的單詞數。
測試代碼刪除結果字典中的最後三個字母只出現在文本字符串中的任何對,我嘗試了下面的函數,但它不起作用。
def get_last_three_letters_dict(sentence):
sentence = dict(sentence)
tails = []
for letter in sentence:
if len(name) > 2:
tails.append(name[-3:].lower())
return (''.join(tails) + ":")
預期:
1.
est : 4
ing : 5
rst : 2
2.
han : 2
his : 2
ing : 2
oes : 2
the : 4
你會期望'dict(句子)'做什麼?對句子調用字典毫無意義。 – abccd