1
我有一本字典ngram_list如下:檢查dict鍵是否是Python中字典中任何其他元素的子字符串?
ngram_list = dict_items([
('back to back breeding', {'wordcount': 4, 'count': 3}),
('back breeding', {'wordcount': 2, 'count': 5}),
('several consecutive heats', {'wordcount': 3, 'count': 2}),
('how often should', {'wordcount': 3, 'count': 2}),
('often when breeding', {'wordcount': 3, 'count': 1})
])
我想通過字典來排序,從最短的單詞計數到最大,然後循環列表,如果該鍵是任何其他的子串項,刪除它
預期輸出(子項):
ngram_list = dict_items([
('several consecutive heats', {'wordcount': 3, 'count': 2}),
('how often should', {'wordcount': 3, 'count': 2}),
('often when breeding', {'wordcount': 3, 'count': 1}),
('back to back breeding', {'wordcount': 4, 'count': 3})
])
什麼是你的最終預期輸出的字典嗎? – Skycc
@Skycc更新對不起 – Lazhar
所以你想你的輸出作爲字典或像dict.items()返回的元組列表?你需要'OrderedDict'來按順序排序的項目 – Skycc