【发布时间】:2020-11-01 00:17:30
【问题描述】:
我需要编写一个名为 updateHand(hand, word) 的函数来执行此操作:
假设 'hand' 包含 word 中的所有字母。换句话说,这 假设无论字母出现在 'word' 中多少次,'hand' 都有 至少和那封信一样多。
更新手:用完给定单词中的字母并返回 新手,没有那些字母。
没有副作用:不修饰手。
word: string hand: dictionary (string -> int) 返回:dictionary (字符串 -> 整数)
我编写了代码,一切正常,除了返回“手”时,它的顺序不同:
updateHand({'u': 1, 'q': 1, 'a': 1, 'm': 1, 'l': 2, 'i': 1}, 'quail')
{'u': 0, 'i': 0, 'm': 1, 'a': 0, 'l': 1, 'q': 0}
谁能给我解决方案,甚至只是提示这个问题,因为我不明白......
【问题讨论】:
-
您好,您是否尝试过使用保留插入顺序的
collections.OrderedDict? -
不,我对 python 并不陌生,抱歉 :)
标签: python dictionary python-3.5