私は、指定された単語にその文字が含まれている場合、リスト内の文字の量を最小限に抑える機能を持っています。これは、関数のデフです:リストの上の偽の反復
word = 'better'
hand = {'b':1, 'r':1, 's':3, 't':2, 'z':1, 'e':3}
def updateHand(hand, word):
handCopy = hand.copy()
for x in word:
print(x)
print(hand)
handCopy[x] = hand.get(x,0) - 1
print(handCopy)
return handCopy
そして、それは出力です:
b
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 2, 'r': 1, 'e': 3}
e
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 2, 'r': 1, 'e': 2}
t
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
t
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
e
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
r
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 0, 'e': 2}
Out[110]: {'b': 0, 'e': 2, 'r': 0, 's': 3, 't': 1, 'z': 1}
なぜ二t
をスキップ私の関数であり、および/またはリストからそれを排除しないのですか?ありがとう!
'handCopy [x] = hand.get(x、0) - 1' - この行を注意深く考えてください。 – user2357112
また、それはリストではありません。 – user2357112
'hand.get( 't')'は '2'です。 – melpomene